Ben said:
Quoth (e-mail address removed):
I don't know what makes you think that. Did you try it? sysread returns
0 on EOF.
Note that you should also be checking defined($res), as sysread returns
undef on errors. Depending on how you've opened the socket and your
system, some errors (EINTR and EAGAIN, specifically) are not in fact
errors, but indications that you need to retry the read.
I think you need to read
perldoc -f sysread
and
man 2 read
before you go much further (the latter assumes you are on Unix; this is
the documentation for the C read function which is what Perl's sysread
calls. If you are on some other system you will need to find the
appropriate documentation for yourself).
In my original post, I mentioned I am using Linux; Slackware in particular.
I have read the perldoc for sysread. The problem is sysread blocks. So,
when the last of the data is transmitted, $res has a value, the data is
processed, and when the loop goes back to read more data, there is none,
and sysread just sits, waiting for more.
So, I figure, the only way to get sysread to do this is with non-blocking.
Using IO::Select, I came up with this:
my $select = IO::Select->new($socket);
while (1) {
if (my $ready = $select->can_read(1)) {
my $res = sysread($socket,$_,4096);
last if(!defined($res)); #never used
print "RES:$res\nLEN:",length($_),"\n$_\n";
}
else { print "!READY\n"; last; }
}
I tried using "can_read(0)", so that it wouldn't wait, but it was too fast
and the "else { print "!READY\n"; last; }" statement would catch. Without
the else statement, the loop never exists. The "last if(!defined($res));"
is never used because the can_read line doesn't enter the loop if there
isn't any data.
I guess this is the best way to get this to work. It just seems a bit
clunky.