in reply to udp recv question

If a UDP socket has an address bound to it, then ICMP errors related to that that socket are passed back to it, eg
use IO::Socket; use strict; use warnings; my $sock = IO::Socket::INET->new( PeerAddr => 'localhost:9999', Proto=>'udp', ) or die "sock new: $!\n"; my $result; $sock->send("abc",0) or die "send: $!\n"; defined($sock->recv($result,1,0)) or die "recv: $!\n";

For a destination port that isn't open, this code outputs:

recv: Connection refused

Dave.

Replies are listed 'Best First'.
Re^2: udp recv question
by smackdab (Pilgrim) on Jun 29, 2004 at 02:57 UTC
    Thanks, that makes sense...

    I just tried your code on an old linux box I have and it worked but doesn't work on windows 2000. I need something to work on both OSes.

    On windows, I get: "recv: Unknown error"
Re^2: udp recv question
by JamesNC (Chaplain) on Jun 29, 2004 at 10:30 UTC
    How can I get "ICMP error back from my UDP message"
    This may work as advertised on your OS ( Linux? ), but on Win2K returns recv: Unknown error. The above link explains why it works for some Linux implementations.

    UDP is a connectionless protocol ( There is not TTL added to the message so ICMP is not guaranteed! ) If you want to see what makes up it message IP Message Formats
    Cheers!
    JamesNC
      This may work as advertised on your OS ( Linux? ), but on Win2K returns recv: Unknown error. The above link explains why it works for some Linux implementations.
      I tested it on Solaris. It should work on any decent OS. The link you gave refers to this working with unconnected sockets, which seems to be a linux-specific feature, but by adding the PeerAddr parameter, perl creates a connected socket, which should allow any errors to be reported by the OS. It looks like even W2K is reporting the returned ICMP, just not with a very helpful error number.

      Of course with UDP and ICMP you're never guaranteed a reply, but that's not due to TTLs - all IP packets, including UDP and ICMP, have TTL fields.

      Dave.