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;

In reply to Re: socket select hangs after client restarts by 7stud
in thread socket select hangs after client restarts by planetjeff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.