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 |