Creating a UDP server using IO::Socket::INET without specifying a port

273 views Asked by At

I am trying to create a UDP server using perl IO::Socket::INET.

I am facing difficulty with using the following parameters:

  • LocalHost => "0.0.0.0"
  • LocalPort => 0

What I am trying to achieve is to:

  • Listen on all NICs, hence the use of 0.0.0.0
  • Have the OS pick a port, hence the use of 0 as the LocalPort

Using the parameters above with TCP, works just fine. When using those parameters for UDP, it doesn't work.

What I found to work with UDP, was specifying a LocalPort to something fixed and not specifying LocalHost at all, however this is not what I need.

The perl code I have is as follows:

my $sock = new IO::Socket::INET(
              LocalHost => "0.0.0.0",
              LocalPort => 0,
              Proto => 'udp') or die "failed $@";

What I use to assess if I achieved what I need or not is by checking the output of lsof -p <pid>. The following table summarizes what I am seeing in my trials:

Protocol LocalHost LocalPort sockport() output lsof -p output
TCP 0.0.0.0 0 48589 TCP *:48589 (LISTEN)
UDP 0.0.0.0 0 0 protocol: UDP
UDP Not specified 50001 50001 UDP *:50001
UDP output of hostname() 0 58066 UDP hostname:58066
UDP output of hostname() 50001 50001 UDP hostname:50001

The TCP trial in the first row's is what I am trying to achieve but for UDP and I am unable to.

0

There are 0 answers