Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

while(select())

by abachus (Monk)
on Oct 16, 2006 at 13:09 UTC ( #578499=perlquestion: print w/replies, xml ) Need Help??

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

Hello again,

I am trying to write a routine to read all the data from a socket or filehandle. The reference is passed to the routine and the read data is returned to the caller. That much is obvious. So far things seem to work ok, but i'd like to be sure this is water tight.

sub Socket_Read { my $Socket = shift; my $ibits = ''; my $Read_Data; vec($ibits,fileno($Socket),1) = 1; while(select($ibits,undef,undef,0.2)) { last if(!sysread($Socket,my $Input,8192)); $Read_Data .= $Input; } return($Read_Data); }

Please, if you know a better way to do this i'm all ears. thank you in advance,



Isaac Close

Replies are listed 'Best First'.
Re: while(select())
by Anonymous Monk on Oct 16, 2006 at 13:49 UTC
    What are you trying to do? Typically one uses select for one (or a combination) of the following:
    • You have more than one handle to read from/write to, and you want to do something with a handle when it comes available.
    • You want to read from/write to a handle if it's available, and do something else if it's not.
    • You want to time out waiting for a handle to become available.
    Your program doesn't do either. It waits at most 0.2 seconds for a single handle to become availble, and then you read from it, without checking whether it actually has data on it, or whether the select timed out. You also don't check whether sysread encountered an error, or eof.

    Since I can't guess the intend of your program, I won't offer an alternative.

Re: while(select())
by caelifer (Scribe) on Oct 16, 2006 at 19:31 UTC
    You can check this thread. I've posted a complete function there that reads messages from socket. Maybe that's what you are looking for.

    BR

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-08-12 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?