http://qs321.pair.com?node_id=42898


in reply to Net::Ping, the mini series

Excellent stuff tye. I just thought I'd something which might help.

You can use connect with UDP sockets - its just an API thing though and it sets the 'default address' to which packets are sent, as well as filtering packets to only be received from that address. That's not very useful, but what is more useful is that you can detect the 'port unreachable' ICMP messages that would be sent back through the normal API. The error ICMP packets which the ip stack receives are associated with the socket and so if you try and send another packet after one of these error packets has been received, you get an error back from the send (although the packet is still sent I think). So, if you know that the host isn't listening on a particular UDP port, then a simple UDP ping can be done without needing any special permissions.

Anyway, try this - it works for me!

#! /usr/bin/perl -w use strict; use IO::Socket::INET; my($host, $port) = qw(localhost 12345); my $sock = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port, Proto => 'udp') || die; my $retry = 6; while ($retry--) { unless (send($sock, "ping!", 0)) { print "Host is up? ($!)\n"; } sleep(1); }

Have fun,

rdw

Replies are listed 'Best First'.
Re: Re: Net::Ping the mini series (UDP Pings)
by Anonymous Monk on Mar 02, 2001 at 18:07 UTC
    it doesn't work for me :( or precisely it's works always: if udp port is up or down ! i need to test for my web servers some ports, I have only problems to test the UDP ones... How can i do that ? Thanks for support sachab@usa.net
      I know this is an old post, but I think I might know why your program in saying 'yes' all the time.

      The reason it might be always reporting that the server is up might be that if it is a linux box, iptables might have icmp host unreachable turned off. If that's the case then when your send call is done, it returns without error and in the gap between the nth and the (n+1)th send call, the OS reports an ICMP host unreachable, but due to the iptables rule, it gets dropped at the point of entry, never making it to the program. So your subsequent sends do not see any errors reported by the previous ones and it looks as if all is well.

        Even for me it does not work on windows as well. But it works for TCP. Can anyone know how to make it work with UDP? Thanks in advance, kuntalm@hotmail.com