programming-examples/perl/Network/UPD inet server.pl

23 lines
659 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!/usr/bin/perl
use warnings;
use strict;
use Socket;
my $proto = getprotobyname('udp');
my $port = 4444;
my $servaddr = sockaddr_in($port, INADDR_ANY);
socket SERVER, PF_INET, SOCK_DGRAM, $proto or die "Unable to create socket: $!";
bind SERVER, $servaddr or die "Unable to bind: $!";
print "Server running on port $port...\n";
my $message;
while (my $client = recv SERVER, $message, 1024, 0) {
my ($port, $ip) = unpack_sockaddr_in($client);
my $host = gethostbyaddr($ip, AF_INET);
print "Client $host:$port sent '$message' at ", scalar(localtime), "\n";
send SERVER, "Message '$message' received", 0, $client;
}