Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

IO::Socket to recv

by dsb (Chaplain)
on Feb 13, 2006 at 15:47 UTC ( [id://529853]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to open a socket to a service, send a message and receive a response. With the following code, I can do just that:
my $sock = IO::Socket::INET->new( PeerAddr => '10.0.0.1', PeerPort => '22222', Proto => 'tcp', ); print "Connection to 22010 is open\n" if $sock->connected(); my $buf; $sock->send( $msg ); $sock->recv( $buf, 200 ); print "Message: $msg\n"; print "Response: $buf\n"; $sock->close();
The trouble is that if the service is not functioning properly it does not respond, and so the call to recv hangs. It's okay if the server doesn't respond (that's kind of the point of this little smoke test), but I need to just get an empty response and move on...

Thoughts?


dsb
This @ISA my( $cool ) %SIG

Replies are listed 'Best First'.
Re: IO::Socket to recv
by McDarren (Abbot) on Feb 13, 2006 at 15:58 UTC
    perldoc -q timeout Found in /usr/share/perl/5.8/pod/perlfaq8.pod How do I timeout a slow event? Use the alarm() function, probably in conjunction with a signal handler, as documented in "Signals" in perlipc and the section on ``Signals'' in the Camel. You may instead use the more flexible Sys::AlarmCall module av +ailable from CPAN. The alarm() function is not implemented on all versions of Wind +ows. Check the documentation for your specific version of Perl.
    That should help.... check the specific docs for alarm and eval for more information.

    Update:Actually, I just had a look and IO::Socket has its own timeout method. So you probably want to use that :p

Re: IO::Socket to recv
by salva (Canon) on Feb 13, 2006 at 16:11 UTC
    for a networking app you would probably want to create your sockets as non-blocking and use the select() builtin to determine when data is available for reading or the timeout has expired.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found