in reply to socket select hangs after client restarts

When I add your for loop to the top of my example, I can't duplicate the hanging you are experiencing. Can you try running the following server as well as two clients in separate windows? At the prompt in the clients, type in some messages and hit return, then observe how the server displays the messages in its terminal window. Then hit ctrl+C in one of the clients. Then enter a message at the prompt in the other client. See if the server displays the message in its terminal window.

Note: I have included some debugging output too, which you will see in the server window.

Note2: recreating the bit string every time through the loop is not very efficient. You should just add new connections to the bit string when new connections are detected, and remove connections from the bit string as connections are deleted (and better yet dispense with all the bit twiddling and use IO::Select).

#SERVER: use strict; use warnings; use 5.010; use Socket; #SERVER: my $protocol = getprotobyname 'tcp'; socket my $LISTEN_SOCK, AF_INET, SOCK_STREAM, $protocol or die "Can't make socket: $!"; setsockopt $LISTEN_SOCK, SOL_SOCKET, SO_REUSEADDR, 1 or die "Cant set SO_REUSADDR: $!"; my $port = 12555; my $listen_addr = sockaddr_in $port, INADDR_ANY; bind $LISTEN_SOCK, $listen_addr or die "bind failed: $!"; listen $LISTEN_SOCK, 5; warn "processing sockets...\n"; my %openStreamsSock; my $streamRequest; my $count = 0; while (1) { # Set up bit vectors for polling my $fin = ''; my $fout; vec ($fin, fileno ($LISTEN_SOCK), 1) = 1; foreach my $streamID (keys %openStreamsSock) { vec ($fin, fileno($openStreamsSock{$streamID}), 1) = 1; } # Wait for incoming message my $nfound = select ($fout=$fin, undef, undef, undef); say "select worked after child ended" if $count == 1; if ($nfound) { if (vec($fout, fileno($LISTEN_SOCK),1)) { say "in 2nd if"; #$openStreamsSock{$streamRequest++} = $LISTEN_SOCK->accept(); my $packed_remote_addr = accept(my $CONNECTION, $LISTEN_SOCK) or warn "Couldn't connect: $!"; say "before 3rd if"; if ($packed_remote_addr) { say 'in 3rd if'; $openStreamsSock{$streamRequest++} = $CONNECTION; } } say 'starting for loop'; foreach my $streamID (keys %openStreamsSock) { say "in for loop after child ended" if $count == 1; if (vec($fout, fileno($openStreamsSock{$streamID}),1)) { # read data off the socket; not a message here, just raw d +ata my $msgSize = sysread ($openStreamsSock{$streamID}, my $msgReceived, 1048576); if ($msgSize > 0) { #writeStreamData ($streamID, $msgReceived); syswrite(STDOUT, $msgReceived); } else { # $msgSize being 0 indicates end of stream, or # $msgSize being undef indicates error, so close #vec($fin, fileno $openStreamsSock{$streamID}, 1) = 0; close ($openStreamsSock{$streamID}); delete ($openStreamsSock{$streamID}); say 'did deleting'; $count = 1; } } } } else { print "$0: Normal timeout of select...\n"; } }
#CLIENT: use strict; use warnings; use 5.010; use Socket; my $protocol = getprotobyname 'tcp'; socket my $SOCK, AF_INET, SOCK_STREAM, $protocol or die "Couldn't create socket: $!"; my $port = 12555; my $host = 'localhost'; my $packed_host = gethostbyname $host or die "Unknown host: $!"; my $sock_addr = sockaddr_in $port, $packed_host; connect $SOCK, $sock_addr or die "couldn't connect: $!"; my $old_out = select $SOCK; $| = 1; select $old_out; print "Enter some text: "; while (my $to_send = <STDIN>) { print $SOCK $to_send; } close $SOCK;

Replies are listed 'Best First'.
Re^2: socket select hangs after client restarts
by planetjeff (Initiate) on Feb 22, 2010 at 23:17 UTC

    Okay - problem has been solved, and I also have a slight confession.

    I actually edited down my program a bit more than I should have when I posted it, and it didn't reflect it's true functionality. When a new socket was opened, I was immediately reading data with a sysread. (the first block of data after an open socket is supposed to contain the filename for the server to write the following data to, so I always assumed the client would be a 'good' client and have that data read to be read on open!) The code was hanging on that particular sysread. Sometimes the client requests an open socket and does not send any data. Why the code would just hang on sysread baffles me, but that is exactly what it was doing. I did have the code to process successfully read data, zero data, and undefined return from sysread, but in this one special cases it would never break out of the sysread function. Why is that?

    My pared down code - and yours, too - gave me the idea to just accept the socket request and put it on the select queue to wait for data to be read. Problem solved. I now wait for data before reading it, and it now handles good and bad clients.

    Many thanks for the help, monks. I hope this in turn can help other who experience the same situation. Don't assume clients will behave!

    p.s. I would be interested if anyone has insight why sysread hangs in this sort of scenario...thx!