#!/usr/bin/perl -w
# unix_socket_server.pl
use Socket;
print "Server Started.\n";
$AF_UNIX=1;
$SOCK_STREAM=1;
$PROTOCOL=0;
socket(SERVERSOCKET,$AF_UNIX,$SOCK_STREAM,$PROTOCOL) ||
	die "Socket $!\n";
print "Socket OK\n";

$name="./.mysock";
unlink "$name" || warn "$name: $!\n";

bind(SERVERSOCKET,sockaddr_un($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";
	defined($pid = fork) || die "Fork: $!\n";
	if ($pid == 0) {
		print(NEWSOCKET "Greetings from your server !!\n");
		close(NEWSOCKET);
		exit(0);
	}
	else {
		close(NEWSOCKET);
	}
}
