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

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

From:
perl5-porters
Date:
April 30, 2004 04:51
Subject:
Re: [perl #29019] nonblocking read returns EAGAIN at eof
Message ID:
c6tejp$321$1@post.home.lunix
In article <200404292122.i3TLM4nW002259@idiom.com>,
	David Muir Sharnoff <muir@idiom.com> 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;
use POSIX qw(F_GETFL F_SETFL O_NONBLOCK);

my $l = IO::Socket::INET->new(LocalAddr => "127.0.0.1",
                              Listen => 5, Blocking => 0) ||
    die "Could not create listening socket: $!";
my ($port) = unpack_sockaddr_in($l->sockname);
my $wr = IO::Socket::INET->new(PeerAddr => "127.0.0.1", 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: $!";
$rd->blocking(0);
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 test.pl
undef/Resource temporarily unavailable

Testing when the connection is closed:
>perl test.pl foo
0/

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).



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About