Sorry that this looks ugly here. click download to see correct format.
sub ListenOnUDP { my $end = time + $timeout; print "PortListen on $strFQDN ($address) at $strProtocol\/$intList +enPort\n"; # Open a udp port my $hSocket = IO::Socket::INET->new(LocalPort=>$intListenPort, Pro +to=>$strProtocol, Timeout=>$timeout) or die "Can't create UDP socket: $@"; # Wait for a connection ($hSocket is undef until an accept call is + made). my $rin = ''; vec($rin, fileno($hSocket), 1) = 1; while (1) { my $timeleft = $end - time; if ($timeleft <= 0) { print "No connection: Timed out after $timeout seconds.\n" +; last; } my ($nfound, $t) = select(my $rout = $rin, undef, undef, $time +left); if ($nfound == 0) { # either timeout or end of file print "No connection: Timed out after $timeout seconds.\n" +; last; } # Connection exists; Receive data, open a response port, respo +nd, then close both ports. my ($datagram,$flags); while ($hSocket->recv($datagram,42,$flags)) { my $ripaddr = $hSocket->peerhost; my $rport = $hSocket->peerport; print "RECIEVE_FROM-TO, $ripaddr, $strProtocol\/$rport, $a +ddress, $strProtocol\/$intListenPort, message=$datagram\n"; # UDP is connectionless, so try to open a connection going + back the other way, then send a response. # NOTE: This requires a predefined 'remote' port. Pass via + commandline; default is $intSendPort my $hResponseSocket = IO::Socket::INET->new(Proto=>$strPro +tocol,PeerHost=>$ripaddr,PeerPort=>$intSendPort); print "SEND_FROM-TO, $address, $strProtocol\/" . $hRespons +eSocket->sockport . ", $ripaddr, $strProtocol\/$intSendPort, message= +PONG!\n"; $hResponseSocket->send("PONG!"); close($hResponseSocket); last; } last; } close($hSocket); }
Again, thanks folks, this solved my problem. Anyone interested in helping me QA and test my final script on other platforms?
In reply to Re^4: Timeout Socket recv on UDP... on windows
by shunyun
in thread Timeout Socket recv on UDP... on windows
by shunyun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |