Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

IO::Socket to recv

by dsb (Chaplain)
on Feb 13, 2006 at 15:47 UTC ( #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

Comment on IO::Socket to recv
Download Code
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 (Monsignor) 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
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? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-09-19 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (147 votes), past polls