The server and the client are on the same machine : Server « Network « Perl






The server and the client are on the same machine

    


#!/bin/perl

print "Server Started.\n";
$AF_UNIX=1;     # The domain is AF_UNIX
$SOCK_STREAM=1; # The type is SOCK_STREAM
$PROTOCOL=0;    # Protocol 0 is accepted as the "correct protocol" by most systems.

socket(SERVERSOCKET, $AF_UNIX, $SOCK_STREAM, $PROTOCOL) || die " Socket $!\n";
print "socket OK\n";
$name="./greetings"; 
unlink "./greetings" || warn "$name: $!\n";

bind(SERVERSOCKET, $name) || die "Bind $!\n";
print "bind OK\n";

listen(SERVERSOCKET, 5) || die "Listen $!\n";
print "listen OK\n";

while(1){
    accept(NEWSOCKET, SERVERSOCKET ) || die "Accept $!\n";
    $pid=fork || die "Fork: $!\n";
    if ($pid == 0 ){
         print (NEWSOCKET "Greetings from your server!!\n";
         close(NEWSOCKET);
         exit(0);
    }else{
        close (NEWSOCKET);
    }
}

                      

   
    
    
    
  








Related examples in the same category

1.Two way server
2.Time server
3.Simple server
4.Socket answer
5.Socket pair
6.Socket server waiting for clients
7.Read from server
8.Send data from client to server
9.Forking Servers