UPD inet server : UDP « Network « Perl






UPD inet server

   

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

   
    
    
  








Related examples in the same category

1.UDP client
2.UDP inet client
3.UDP server