Socket server waiting for clients : Server « Network « Perl






Socket server waiting for clients

    

use IO::Socket;

$server = IO::Socket::INET->new
(
    LocalPort => 1247,
    Type      => SOCK_STREAM,
    Reuse     => 1,
    Listen    => 5 
) or die "Could not create server.\n";

while ($client = $server->accept()) {

    unless (defined($child_pid = fork())) {die "Can not fork.\n"};

    if ($child_pid) {

        while ($line = <$client>) {

            print "Read this from client: $line";

        }

    } else {

        while ($line = <>) {

            print $client $line; 

        }
    }    
}

   
    
    
    
  








Related examples in the same category

1.Two way server
2.Time server
3.The server and the client are on the same machine
4.Simple server
5.Socket answer
6.Socket pair
7.Read from server
8.Send data from client to server
9.Forking Servers