Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sysread returning 0 when used with sockets

by Zubinix (Acolyte)
on Mar 16, 2011 at 04:01 UTC ( [id://893490]=perlquestion: print w/replies, xml ) Need Help??

Zubinix has asked for the wisdom of the Perl Monks concerning the following question:

Hi all monks!

I am having trouble using sysread with a standard INET soocket created using IO::Socket::INET. I am trying to read data from a server that sends data in chunks about 1 minute apart then optionally after receiving a chunk of data I might want to send something back to the server. Hence I need to use a sysread with a timeout.

However, my problem is that though I can receive the first block of data, the second block of data which arrives after a minute results in sysread returning 0 which I believe indicates EOF (socket closed?). The results is the same if I use IO::Select or an alarm construct such as:

eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $rx_timeout; $ncount = sysread($client, $data , 1); alarm 0; };

On reading the second block of data sysread will exit immediately and always return 0.

If I don't implement any mechanism to break out of the sysread (including using select statements) then sysread work fine and is able to read subsequent data blocks sent from the server but it does block while doing this which is not what I want.

Any ideas on how to make sysread work in this situation in a non-blocking manner?
Thanks!

Replies are listed 'Best First'.
Re: Sysread returning 0 when used with sockets
by mr_mischief (Monsignor) on Mar 16, 2011 at 05:08 UTC

    If you've read all that is written to a file, aren't you at the end of the file?

    sysread returns undef if there's an error. Being at the end of the file is a normal condition, especially for a pipe or socket. You might find some information on sysread by looking at some docs for the underlying read(2) system call, such as this from the docs for it on my system:

    RETURN VALUE

    On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.

    On what, exactly, are you basing your time at which to read? Are you sure your timer and the timer on the server are set to the exact number of seconds and starting at the same time? If the server hasn't written new data yet, there won't be any new data to read yet.

Re: Sysread returning 0 when used with sockets
by ikegami (Patriarch) on Mar 16, 2011 at 06:22 UTC

    You're claiming there's a bug — sysread is returning zero when the socket isn't closed — but you're asking us to do the work of demonstrating a bug that hasn't been encountered before in a commonly used function.

    How about providing a minimal runnable demonstration of the issue?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://893490]
Approved by ikegami
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-30 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found