Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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 => '', 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...


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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-12-04 19:43 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.