Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Deteriming how much is left to read on a filehandle

by reptile (Monk)
on Jun 18, 2000 at 04:09 UTC ( #18688=note: print w/ replies, xml ) Need Help??


in reply to Deteriming how much is left to read on a filehandle

I've wondered the same thing. First, for filehandles, I know you can use -s HANDLE to return the size of the file. If you use the read to read from the file, it will return the number of bytes read, and you can subtract that from another variable you initially set to the file size. Unfortunately, this (as far as I know) only works for files.

I've tried something similar on sockets and -s just doesn't return anything unless there's data. If there isn't any, it waits until there is, which could be never. So, to conditionally read from the socket if there was any data on it, I installed an SIGALRM handler to timeout any read operations after 1 second. Of course if I didn't want it to pause that would just totally suck. Anyway, I'm rambling, but I know of no way to check how much data is waiting on a socket. Still, read does return how much data was read from a socket, but someone please correct me if I'm wrong, it will continue to read, or try to read, until it reads either an EOF or however many bytes you tell it to read. Plus you have no way of knowing how many bytes are waiting to be read in the first place, and thus no way to know how much is left.

I'm going to vote up your node because I really really want an answer to this myself, and for some reason it hasn't landed on the SOPW page yet.

72656B636148206C72655020726568746F6E41207473754A


Comment on Re: Deteriming how much is left to read on a filehandle
RE: Re: Deteriming how much is left to read on a filehandle
by nardo (Friar) on Jun 18, 2000 at 23:01 UTC
    someone please correct me if I'm wrong, it will continue to read, or try to read, until it reads either an EOF or however many bytes you tell it to read

    If your platform supports it, you can use fcntl to set the O_NONBLOCK flag on a socket, which will make read return even if it was not able to read as many bytes as you tell it to read.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://18688]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2014-08-02 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls