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