in reply to Re^2: Timeout Socket recv on UDP... on windows
in thread Timeout Socket recv on UDP... on windows
Caveat: I've had little occasion to do much with udp sockets. See also Perl Cookbook (I hope that is a sanctioned link)
However, udp is connectionless, so you do not need an accept loop. Essentially you want to wait for upto a specified number of seconds for a datagram to arrive and do something else if not. So,
my $hSocket = IO::Socket::INET->new( LocalPort=>$intListenPort, Proto=>$strProtocol ) or die "Can't create UDP socket: $@"; ioctl($hSocket, 0x8004667e, pack("I", 1)); my( $gotone, $datagram, $flags) = 0; my $endtime = time() + $timeout; while( time() < $endtime ) { sleep 1 and next unless $hSocket->recv($datagram,42,$flags ); ## Got a datagram. ... $gotone = 1; last; } if( $gotone ) { ## We processed a datagram within the timelimit } else { ## we didn't }
There are probably better ways of structuring that. And you could probably use select or IO::Select to achieve a similar thing.
Or threads.
|
|---|