programming-examples/perl/Network/The server and the client are on the same machine.pl

29 lines
811 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!/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);
}
}