X
xchris
I'm really new to perl. (2 days)
i'm getting crazy for a simple server that should fork for each connection
...say "Hi Mon" and then close connection.
i paste my code.Can you help me? 10x a lot
i took part of the code from a book and the net.
bye
xchris
use IO::Socket;
use POSIX qw
sys_wait_h);
sub REAPER {
1 until (-1 == waitpid(-1,WNOHANG));
$SIG{CHLD} = \&REAPER;
}
socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
setsockopt(SERVER,SOL_SOCKET,SO_REUSADDR,1);
$my_addr=sockaddr_in("9999",INADDR_ANY);
bind (SERVER,$my_addr)
or die "can't bind";
listen (SERVER,SOMAXCONN)
or die "Cant listen";
$SIG{CHLD}= \&REAPER;
print ("Listening\n");
while ($client_addr = accept(CLIENT,SERVER)) {
## ok new connection... let's fork
$pid = fork;
die "fork: $!" unless defined $pid;
print "Pid: $pid\n";
if ($pid == 0 )
{
## we're inside child
print CLIENT "Hi Mon!\n";
exit;
}
}
close (SERVER);
i'm getting crazy for a simple server that should fork for each connection
...say "Hi Mon" and then close connection.
i paste my code.Can you help me? 10x a lot
i took part of the code from a book and the net.
bye
xchris
use IO::Socket;
use POSIX qw
sub REAPER {
1 until (-1 == waitpid(-1,WNOHANG));
$SIG{CHLD} = \&REAPER;
}
socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
setsockopt(SERVER,SOL_SOCKET,SO_REUSADDR,1);
$my_addr=sockaddr_in("9999",INADDR_ANY);
bind (SERVER,$my_addr)
or die "can't bind";
listen (SERVER,SOMAXCONN)
or die "Cant listen";
$SIG{CHLD}= \&REAPER;
print ("Listening\n");
while ($client_addr = accept(CLIENT,SERVER)) {
## ok new connection... let's fork
$pid = fork;
die "fork: $!" unless defined $pid;
print "Pid: $pid\n";
if ($pid == 0 )
{
## we're inside child
print CLIENT "Hi Mon!\n";
exit;
}
}
close (SERVER);