You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
659 B
Perl

#!/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;
}