develooper Front page | perl.perl5.porters | Postings from April 2004

Re: [perl #29019] nonblocking read returns EAGAIN at eof

April 30, 2004 04:51
Re: [perl #29019] nonblocking read returns EAGAIN at eof
Message ID:
In article <>,
	David Muir Sharnoff <> writes:
> If I use sysread() intead of read() then I can't use eof() at all
> because eof does getc()/ungetc() internally.  I don't see any reason
> for perl's read() to return EAGAIN at eof().

eof() is still buffered I/O, personally I wouldn't use that with
non-blocking sockets, but that's besides the point here.
> The man page for fread() says that it should return 0 at EOF.
For me (linux, 5.8.2) and using read() instead of sysread() things still
behave as expected. I used this to test:

#! /usr/bin/perl -wl
use strict;
use IO::Socket::INET;

my $l = IO::Socket::INET->new(LocalAddr => "",
                              Listen => 5, Blocking => 0) ||
    die "Could not create listening socket: $!";
my ($port) = unpack_sockaddr_in($l->sockname);
my $wr = IO::Socket::INET->new(PeerAddr => "", PeerPort => $port) ||
    die "Could not create connecting socket: $!";
sleep 1; # Give connection a bit of time;
my $rd = $l->accept || die "Could not accept connection: $!";
close($wr) if @ARGV;
sleep 1; # Give EOF chance to propagate
my $rc = read($rd, my $buf, 1);
#my $rc = eof($rd);
print defined($rc) ? $rc : "undef", "/$!";

Testing when the connection isn't closed:
> perl
undef/Resource temporarily unavailable

Testing when the connection is closed:
>perl foo

Both are exactly as expected.

However, if I replace the read() by the eof() test, i get:
1/Illegal seek
in both cases, which i think *IS* a bug (I'm reporting it
seperately from this, since I think it's different from what
you are talking about). Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About