in reply to Re: socket select hangs after client restarts
in thread socket select hangs after client restarts

Many thanks for your response - let me take this by the numbers:

1,2,3) It was actually a typo, caused by me editing out superfluous code/comments/debugs, and a manual typing error. That line should have been:

$openStreamsSock{$streamRequest++} = $sock->accept();

$streamRequest is just stream counter. New client connection sockets are added to the hash, with the $streamRequest the unique key.

4) Thanks for the coding economy!

5) You're right, I wasn't depopulating $fin (now $rin, to match standard), but at the top of the loop I reinitialized $fin and then set it up again by adding the listening socket and all currently open sockets. That should have taken care of initializing it correctly each time I get to the select.

Here's the updated code, adding in checking EBITS:

while (1) { # Set up bit vectors for polling my $rin = ''; my $ein = ''; my $rout; my $eout; vec ($rin, fileno ($sock) , 1) = 1; vec ($ein, fileno ($sock) , 1) = 1; foreach my $streamID (keys %openStreamsSock) { vec ($fin, fileno($openStreamsSock{$streamID}) , 1) = 1; vec ($ein, fileno($openStreamsSock{$streamID}) , 1) = 1; } # Wait for incoming message my $nfound = select ($rout=$rin, undef, $eout=$ein, 5); if ($nfound) { # Check client streams, close any in error foreach my $streamID (keys %openStreamsSock) { if (vec($eout, fileno($openStreamsSock{$streamID}),1)) { close ($openStreamsSock{$streamID}); delete ($openStreamsSock{$streamID}); } } if (vec($rout, fileno($sock),1)) { $openStreamsSock{$streamRequest++} = $sock->accept(); } else { foreach my $streamID (keys %openStreamsSock) { if (vec($rout, fileno($openStreamsSock{$streamID}),1)) { # read data off the socket; not a message here, just r +aw data $msgSize = sysread ($openStreamsSock{$streamID}, $msgReceived, 1048576); if (defined ($msgSize) && ($msgSize > 0)) { writeStreamData ($streamID, $msgReceived); } else { # $msgSize being 0 indicates end of stream, or # $msgSize being undef indicates error, so close close ($openStreamsSock{$streamID}); delete ($openStreamsSock{$streamID}); } } } } } else { print "$0: Normal timeout of select...\n"; } }

I'll have to look at your example a bit - it looks like you're using some different constructs. Thanks again!