http://www.perlmonks.org?node_id=100571


in reply to socket reading...

You could put your socket into non-blocking mode and actually look at the return value from sysread. If your responses over the socket are record-oriented and terminated by a particular character (like, say, "\n") you could set $/ to that character and use the <$sock> form. Otherwise, if you have a length indication at the beginning of each response, read that first and then read exactly the right number of bytes.

update: corrected part about blocking if you try to read too many bytes.

Replies are listed 'Best First'.
Re: Re: socket reading...
by kschwab (Vicar) on Jul 28, 2001 at 21:28 UTC
    I don't agree with this assumption:

    When select comes back to you it means that some data is available; it doesn't say how much. Then you try to read 1024 bytes. If there isn't that much data available, and you're in blocking mode, it'll try to wait.

    read() and/or sysread() will read whatever is there without waiting on the remainder of the 1024 bytes. They will block if there's no data to read, but select() says there is data.