Example of a Perl TCP server using Socket module. : TCP « Network « Perl






Example of a Perl TCP server using Socket module.

    


#!/usr/bin/perl -w  
  
use IO::Socket;  
  
$port = 9999;  

$server_socket = IO::Socket::INET->new(
     LocalPort => $port,
     Listen    => SOMAXCONN,
     Proto     => 'tcp',
     Reuse     => 1)
     or die "Cannot open socket: $!";
         

print "Server listening on port $port\n";  
  
while ( $client_socket = $server_socket->accept() ) {
    $client_host = $client_socket->sockhost();
    print $client_socket "Hello from server.\n";
    print "Sent message to client on $client_host.\n";
    $client_socket->close();
}

   
    
    
    
  








Related examples in the same category

1.TCP client
2.TCP client using Socket module.
3.TCP inet server
4.TCP server
5.tcp inet client
6.Sample TCP client without the Socket module.
7.Sample TCP client.
8.Simple TCP Clients