Home > Net >  can't detect closed connection with $sock->connected() [duplicate]
can't detect closed connection with $sock->connected() [duplicate]

Time:09-17

I'm trying - and failing - to get a perl server to detect and get rid of connection with a client who broke the connection. Everywhere I looked, the suggested method is to use the socket's ->connected() method, but in my case it fails.

This is the server absolutely minimized:

#!/usr/bin/perl
use IO::Socket;
STDOUT->autoflush(1);


my $server = new IO::Socket::INET (
                                Listen => 7,
                                Reuse   => 1,
                                LocalAddr => '192.168.0.29',
                                LocalPort => '11501',
                                Proto => 'tcp',
                                 );
die "Could not create socket: $!\n" unless $server;

print "Waiting for clients\n";

while ($client = $server->accept()) {
        print "Client connected\n";
        do {
                $client->recv($received,1024);
                print $received;
                select(undef, undef, undef, 0.1); # wait 0.1s before next read, not to spam the console if recv returns immediately
                print ".";
        } while( $client->connected() );
        print "Client disconnected\n";
}

I connect to the server with Netcat, and everything works fine, the server receiving anything I send, but when I press ctrl-C to interrupt Netcat, 'recv' is no longer waiting, but $client->connected() still returns a true-like value, the main loop never returns to waiting to the next client.

(note - the above example has been absolutely minimized to show the problem, but in the complete program the socket is set to non-blocking, so I believe I can't trivially depend on recv returning an empty string. Unless I'm wrong?)

CodePudding user response:

connected can't be used to reliably learn whether the peer has initiated a shutdown. It's mentioned almost word for word in the documentation:

Note that this method considers a half-open TCP socket to be "in a connected state". [...] Thus, in general, it cannot be used to reliably learn whether the peer has initiated a graceful shutdown because in most cases (see below) the local TCP state machine remains in CLOSE-WAIT until the local application calls "shutdown" in IO::Socket or close. Only at that point does this function return undef.

(Emphasis mine.)

If the other end disconnected, recv will return 0. So just check the value returned by recv.

while (1) { 
   my $rv = $client->recv(my $received, 64*1024); 
   die($!) if !defined($rv);     # Error occurred when not defined.
   last if $received eq "";      # EOF reached when zero.

   print($received);
}

Additional bug fix: The above now calls recv before print.

Additional bug fix: Removed the useless sleep. recv will block if there's nothing received.

Performance fix: No reason to ask for just 1024 bytes. If there's any data available, it will be returned. So you might as well ask for more to cut down on the number of calls to recv and print.

Note that even with this solution, an ungraceful disconnection (power outage, network drop, etc) will go undetected. One could use a timeout or a heartbeat mechanism to solve that.

  • Related