in reply to Knowing when a socket connection is dead

I had the same problem just recently. This is how I resolved it.
sub socketListen { my $client = shift; # specific client connection execut +ed in this thread my $server = shift; my $server_port = shift; # port number of currently connected s +ocket # data is ready to be received over the socket # $client->autoflush(); print "connected to port: $server_port\n" if ( $debug == 1 ); my @recData = (); my $numTries = 0; # set connection up for non-blocking read # my $true=1; my $rec; ioctl($client, 0x8004667e, \$true) or return -1; while ( $quitThread == 0 ) { # try to read data # $rec = undef; my $res = sysread($client, $rec, $bufferSize); if ( defined($res) ) { if ( $res == 0 ) { # socket was closed remotely # print "received nothing on socket $server_port.\n" if +( $debug == 1 ); last; } elsif ( $res > 0 ) { # received message # print "received on socket $server_port:$rec\n" if ( $d +ebug == 1 ); } } else { # no new information came through # print "sleeping: received nothing on socket $server_port.\ +n" if ( $debug == 1 ); sleep 2; next; } } $client->close; print "closing socket $server_port...\n" if ( $debug == 1 ); }
This was the body of a sub routine I called whenever my selector could do a "can_read" on the connection. You may want to use the "numTries" to have the connection timeout if nothing comes through over a certain time period.