Re: sysread & eof




Quoth lstouder@xxxxxxxxxxxx:
I'm use IO::Socket on linux to read from a webpage. I don't want to use
LWP::Simple. How would I find the EOF when the web server is finished
sending me the webpage? Here's what I'm using right now:

while (my $res = sysread($sock_in,$buf,$bufsize)) {
syswrite($sock_out,$buf,length($buf));
last if ($res < $bufsize);
}

It works, but obviously, if a webpage has a size that is a multiple of
$bufsize, this will loop forever.

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

Ben

--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
ben@xxxxxxxxxxxx Frank Herbert, 'Dune'
.



Relevant Pages

  • Re: Ruby byte access to disk sectors like dd does
    ... All of my file accesses are sysread, syswrite, and sysseek, which are not compatible with IO#eof. ... Using the file size to prevent hitting the EOF does not work for /dev/sdb. ... reading a single byte at a time might be easier for the implementation but it is almost inevitably dramatically slower than reading in chunks. ...
    (comp.lang.ruby)
  • Re: sysread & eof
    ... I have read the perldoc for sysread. ... It blocks waiting on more data. ... If that is actually the case, then the server is not sending an eof when it ...
    (comp.lang.perl.misc)
  • Re: Read socket using both <> and sysread()
    ... x> Is the fact that read will block until the requested size (or eof) ... x> is read (as opposed to sysread, which blocks until at least 1 byte ... but after that will return a partial buffer if need be ... return a partial read or some EINTR error instead. ...
    (comp.lang.perl.misc)
  • Re: TCP client/server speaking proprietary protocol
    ... In general it is worth avoiding eof(). ... have returned true. ... freadand read) are not in fact fatal errors and just require you ... I've seen sysread of course ...
    (comp.lang.perl.misc)