Hi all - first time poster. One of my tcp server modules of a larger program is designed to be able to read from many tcp client connections at the same time. I do this with hash tables and a socket select, so no forking is involved. However, a major anomaly is occurring where, if the client is in the middle of writing data to my server and the client restarts, my server ends up hanging and cannot service any more connections, new or already open. Here is a code snippet:
while (1) { # Set up bit vectors for polling my $fin = ''; my $fout; vec ($fin, fileno ($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, 5); if ($nfound) { if (vec($fout, fileno($sock),1)) { $openStreamsSock($streamRequest++) = $sock->accept(); } else { foreach my $streamID (keys %openStreamsSock) { if (vec($fout, 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"; } }
This code usually works like gangbusters, and I have no trouble processing multiple client streams at the same time. However, if the client is unexpectedly shut down, my code seems to just hang and won't accept any more connections or process data on any currently open connections. Originally, I didn't have the timeout of 5 seconds in the select, but I added it in for debug. And what I noticed is that once the client is killed, even the default timeout stops occurring. It's as if the select stops working altogether. I'm hoping some other eyes would shed light on what could be going wrong here. Any thoughts? Thanks much for your consideration.

In reply to 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.