Using UDP Sockets with IO::Socket

Naturally, IO::Socket provides support for UDP sockets. To create a socket suitable for outgoing messages, call IO::Socket::INET->new() with a Proto argument of " udp " and no other arguments:

my $sock = IO::Socket::INET->new (Proto=>'udp') or die $@;

To create a socket bound to a known local port or interface address, provide one or more of the LocalAddr and LocalPort arguments:

my $sock = IO::Socket::INET->new (Proto   => 'udp',
                                  LocalAddr => 12000,
                                  LocalPort => 'localhost'
                                  ) or die $@;

You may also connect() the socket and set a default destination address for send() by providing new() with the PeerAddr and optionally PeerPort arguments:

 my $sock = IO::Socket::INET->new (Proto =>'udp', PeerAddr=>'wuarchive.wustl.edu:daytime(17)' ...

Get Network Programming with Perl now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.