tags:

views:

720

answers:

2

Hi all,

I m doing a sysread in Perl 5.8.2 on AIX 5.3. As per the documentation, sysread is supposed to give 0 when it has read all read from filehandle.

In my scenario, the filehandle is STDIN and points to a socket. So I m basically doing a sysread from a socket. But I never get 0 from sysread and it just blocks, even after client has sent all data.

Any idea what might be wrong?

Thanks.

+5  A: 

What do you mean "sent all data"? sysread returns zero when the handle encounters an end-of-file condition, not when there's no more data available right now. For a socket, EOF on read occurs when the other side has shutdown the socket for write. (Well, and sysread will also return undef, which is numerically equal to zero, if an error occurs such as a network timeout.)

hobbs
i have sysread in a while loop to read data from a socket..but it keeps blocking even after my client has sent all data..sysread just blocks..
someguy
Yes, you've already said that.
hobbs
Your client is obviously not closing it's output filehandle, `sysread()` isn't psychic.
Hasturkun
But i need to keep the socket open..because server has to send some data..only then i can close it..if i close the socket..then sysread works..i need to make sysread non - blocking..
someguy
If this is a socket, client can `shutdown(socket, 1)` to signal it will not write anymore. sysread should return 0 after this occurs, as this answer suggests.
Hasturkun
i added Blocking => 0 on the server socket..and yay! it worked..
someguy
+1  A: 

Check out the select command (the one with the 4 arguments). It can tell you whether there is any input to be read on your filehandle.

mobrule