You seem to be talking about TCP stream oriented connections. In that case, from a berkeley sockets perspective, the EOF is usually detected by a successful read of zero bytes from read(), recv(), or sysread().

Here's a short example of a tcp server that handles multiple connections and detects when they are closed. No warranties, I skipped some error checking to keep it concise. Hope it helps.

#!/usr/bin/perl -w use strict; use IO::Select; use IO::Socket; my $s = IO::Select->new(); my $listener=IO::Socket::INET->new(Listen => 5, LocalPort => 9999, Proto => 'tcp'); $s->add($listener); my $buf; while (1) { # loop thru readable sockets for ($s->can_read()) { # readable event on a listener means we have a new # socket connection if ($_ == $listener) { # accept the connection and add to the select # object print "Connection...groovy\n"; my $newsock=$listener->accept; $s->add($newsock); } else { # this is a "regular" socket, not the listener # try and read up to 512 bytes from it my $return=$_->sysread($buf,512); if (!defined($return)) { # undef from sysread means an error print "error: $!\n"; $s->remove($_); $_->close; } elsif ($return == 0) { # 0 from sysread means the connection was closed print "socket was closed\n"; $s->remove($_); $_->close; } else { # a positive int from sysread means # we got some data # change non-printables to a . char $buf =~tr/\0-\37\177-377/./; print "read $return bytes from a socket [$buf]\n"; } } } }

In reply to Re: Knowing an IO::Socket handle has by kschwab
in thread Knowing an IO::Socket handle has reached end-of-file by dash2

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.