networking intermediate

How to create a simple UDP client in Perl?

Question

How to create a simple UDP client in Perl?

Creating a simple UDP client in Perl involves using the IO::Socket::INET and Socket core modules to construct a UDP socket and send datagrams. The main gotcha is that UDP sockets are connectionless, so when sending data you must supply the packed destination address using sockaddr_in with a properly converted IP address via inet_aton.

In sandboxed environments (without network or filesystem access), trying to send UDP packets may silently fail or time out. To ensure your code runs successfully in such a restricted environment without hanging, avoid waiting for a UDP response, since no server will respond. Instead, just demonstrate creating the socket and sending the message. This will prove the syntax and Perl networking primitives work while avoiding timeouts.

Minimal Working UDP Client Example


use strict;
use warnings;
use IO::Socket::INET;
use Socket qw(sockaddr_in inet_aton);

# Autoflush output immediately
$| = 1;

my $server_addr = '127.0.0.1';
my $server_port = 12345;
my $message     = "Hello UDP server!";

# Create an unconnected UDP socket
my $socket = IO::Socket::INET->new(Proto => 'udp')
  or die "Cannot create UDP socket: $!";

print "Sending message to $server_addr:$server_port...\n";

# Pack the remote address and port
my $sockaddr = sockaddr_in($server_port, inet_aton($server_addr));

# Send the message to the destination address
my $sent = $socket->send($message, 0, $sockaddr);

if (defined $sent) {
    print "Sent $sent bytes successfully.\n";
} else {
    die "Send failed: $!";
}

$socket->close();

print "UDP client finished.\n";

Explanation and Perl Concepts

  • IO::Socket::INET lets you create a UDP socket by setting Proto => 'udp'. No need to connect since UDP is connectionless.
  • The Socket module exports sockaddr_in and inet_aton, which build packed address structures needed for sending UDP datagrams to specific IP/port.
  • Using $socket->send($msg, 0, $sockaddr) sends UDP data to the given address. The 3-arg send method is specific for socket programming.
  • $|=1 flushes STDOUT immediately so printed output appears live, useful for interactive runs or logs.
  • The example deliberately omits waiting on responses since that can cause timeouts in sandboxed or no-network environments.

Common Pitfalls

  • Always use Socket before calling sockaddr_in and inet_aton, as they are exported from Socket.
  • Attempting to recv on UDP sockets without a server running will block/hang unless non-blocking mode is set or a timeout is used.
  • Sending UDP does not guarantee delivery; packets may be dropped silently, especially in sandboxed environments.

Version Notes

  • IO::Socket::INET and Socket are available in all modern Perl 5 versions.
  • No special system calls or modules beyond core modules are used, ensuring compatibility and fast execution.

This minimal example runs immediately and cleanly with perl - in any environment, including sandboxes, demonstrating the basics of UDP client creation and sending with Perl.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 28ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Sending message to 127.0.0.1:12345...
Sent 17 bytes successfully.
UDP client finished.
STDERR
(empty)

Was this helpful?

Related Questions