Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Not able to timeout

by anshumangoyal (Scribe)
on Jul 17, 2012 at 12:12 UTC ( #982217=perlquestion: print w/replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I am using the following code. I want to timeout and close the connection after 20 seconds, tried with alarms but nothing worked. Here is my code:
my $socket_resp = IO::Socket::INET->new(Blocking => 0, LocalPort => $c +omm_port, Proto => 'udp', Timeout => 2); $socket_resp->setsockopt(SO_RCVTIMEO, SO_RCVTIMEO, 10); print "Waiting for Response On Port $comm_port\n"; while (my $recieved_data = $socket_resp->getline()) { chomp($recieved_data); print "$recieved_data\n"; if ($recieved_data =~ m/^done/i) { last; } } $socket_resp->close();
alarms, and nothing else has worked. Can any one help.

Replies are listed 'Best First'.
Re: Not able to timeout
by zentara (Archbishop) on Jul 17, 2012 at 12:58 UTC
Re: Not able to timeout
by davido (Archbishop) on Jul 17, 2012 at 16:22 UTC

    I recently found success with Sys::SigAction, which facilitates alarm timeouts even in system calls that can prove to be stubborn at times.


Re: Not able to timeout
by dolmen (Beadle) on Jul 18, 2012 at 14:40 UTC

    Once you begin to deal with I/O and timers, it's time to use a dedicated that will make your code more reliable, and easier to read and maintain.

    I strongly suggest AnyEvent.

    Try this (incompletely tested) code:

    use strict; use warnings; use AnyEvent; use AnyEvent::Handle::UDP; my $cv = AE::cv; my $comm_port = $ARGV[0] or die "missing port number"; my $server = AnyEvent::Handle::UDP->new( bind => [ localhost => $comm_port ], on_recv => sub { my ($datagram, $handle, $addr) = @_; if ($datagram =~ /^done/i) { $cv->send; } else { print $datagram; } }, rtimeout => 10, on_rtimeout => sub { warn "Timeout!\n"; $cv->send +} ); print "Waiting for Response On Port $comm_port\n"; $cv->recv;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982217]
Front-paged by NetWallah
[Eily]: google tells me 1662 km ...
[LanX]: Nancy? Somewhere near Metz ...
[Eily]: must be km farenheit or something :P
[Eily]: wow, don't say that in Nancy LanX :P
[erix]: maybe you used the car routes, I used the walking routes
[Eily]: there's a rivalry between the two cities, so defining one in terms of the other might not be well received
[Eily]: erix well I did click on the walking icon
LanX giggles!
[LanX]: Eily I'm duing this constantly with people from rival cities ...MUCHO fun!
[Eily]: The best example of that is the TGV, they couldn't decide who would get it so it's in the middle of nowhere halfway between the two

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2017-12-13 15:22 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (369 votes). Check out past polls.