Socket pair : Server « Network « Perl






Socket pair

    

use Socket;
use IO::Handle;

socketpair(CHILDHANDLE, PARENTHANDLE, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
    or  die "Could not create socketpair.";

CHILDHANDLE->autoflush(1);
PARENTHANDLE->autoflush(1);

if ($pid = fork) {
    close PARENTHANDLE;
    print CHILDHANDLE "the parent!\n";
    $line = <CHILDHANDLE>;
    print "Parent: $line";
    close CHILDHANDLE;
    waitpid($pid,0);
} else {
    close CHILDHANDLE;
    $line = <PARENTHANDLE>;
    print "Child : $line";
    print PARENTHANDLE "from the child!\n";
    close PARENTHANDLE;
    exit;
}

   
    
    
    
  








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 server waiting for clients
7.Read from server
8.Send data from client to server
9.Forking Servers