There is something more serious than this infinite loop concern.
I am wondering how did you determine whether a pair of client/server still connected? Although you have a $connected, but that variable was only set once after the connection was established, and after that there was no check any more. Is this $connected meaningful? No, not at all, the connection can be half way shut down by either peer, but $connected is always TRUE.
IO::Socket::INET has a method called ->connected, can you use that one to check, again, it is a NO. That variable is only set once after the connection is established, there is no checking afterwards, unless locally close the socket. (play with the attached code, it demos this.)
In order to detect whether the connection is alive, you have to define your own "heart beat" mechanism, also define the maximum amount of heart beat lost you can tolerate, after that just take it as a dead connection.
server.pl:
use IO::Socket::INET;
$server = new IO::Socket::INET(Timeout => 20, Proto => "tcp", LocalPor
+t => 3000, Listen => 5)
|| die "failed to establish socket\n";
$client = $server->accept;
while (1) {
sleep(2);
if ($client->connected) {
print "still connected\n";
} else {
print "disconnected\n";
}
}
client.pl:
use IO::Socket::INET;
$client = new IO::Socket::INET(Timeout => 20, Proto => "tcp", PeerPort
+ => 3000, PeerAddr => "localhost")
|| die "connection failed";
while (1) {
sleep(2);
if ($client->connected) {
print "still connected\n";
} else {
print "disconnected\n";
}
}