I had the same problem just recently. This is how I resolved it.
sub socketListen { my $client = shift; # specific client connection execut +ed in this thread my $server = shift; my $server_port = shift; # port number of currently connected s +ocket # data is ready to be received over the socket # $client->autoflush(); print "connected to port: $server_port\n" if ( $debug == 1 ); my @recData = (); my $numTries = 0; # set connection up for non-blocking read # my $true=1; my $rec; ioctl($client, 0x8004667e, \$true) or return -1; while ( $quitThread == 0 ) { # try to read data # $rec = undef; my $res = sysread($client, $rec, $bufferSize); if ( defined($res) ) { if ( $res == 0 ) { # socket was closed remotely # print "received nothing on socket $server_port.\n" if +( $debug == 1 ); last; } elsif ( $res > 0 ) { # received message # print "received on socket $server_port:$rec\n" if ( $d +ebug == 1 ); } } else { # no new information came through # print "sleeping: received nothing on socket $server_port.\ +n" if ( $debug == 1 ); sleep 2; next; } } $client->close; print "closing socket $server_port...\n" if ( $debug == 1 ); }
This was the body of a sub routine I called whenever my selector could do a "can_read" on the connection. You may want to use the "numTries" to have the connection timeout if nothing comes through over a certain time period.

In reply to Re: Knowing when a socket connection is dead by metalgear119
in thread Knowing when a socket connection is dead by Forsaken

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.