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";
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.