millionth socket problem....
- From: ".rhavin grobert" <clqrq@xxxxxxxx>
- Date: 23 Jan 2006 11:08:47 -0800
hi there...
Still trying to understand perl-socket-programing, I copyied a short
skript from the perl cookbook. To test it, I checked it with telnet, so
my client *is* working.
My server is the following, and ... it doesn't work (at least not as
expected):
*****************************
#!/usr/bin/perl
use POSIX qw(:sys_wait_h);
use Socket;
# make the socket
socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
# so we can restart our server quickly
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
# build up my socket address
$my_addr = sockaddr_in('23', INADDR_ANY);
bind(SERVER, $my_addr) or die "Couldn't bind to port 23 : $!\n";
# establish a queue for incoming connections
listen(SERVER, SOMAXCONN) or die "Couldn't listen on port $server_port
: $!\n";
sub REAPER {
1 until (-1 == waitpid(-1, WNOHANG));
$SIG{CHLD} = \&REAPER; # unless $] >= 5.002
}
$|=1;
$SIG{CHLD} = \&REAPER;
print 'setting up server'."\n";
while ($hisaddr = accept(CLIENT, SERVER)) {
print CLIENT "\n\r*** both talking ***\n\r";
print 'forking'."\n";
my $CPID=0;
next if $CPID = fork; # parent
print CLIENT "\n\r*** child talking ***\n\r";
die 'fork: '."$!" unless defined $CPID; # failure
# otherwise child
close(SERVER); # no use to child
$|=1;
print 'client got'."\n";
print CLIENT "\n\r*** hi & good bye! ***\n\r";
print '\n*** closing client ***'."\n";
exit; # child leaves
} continue {
print CLIENT "\n\r*** parent talking ***\n\t";
print 'parent closing client...';
close(CLIENT); # no use to parent
print '...parent done!'."\n";
}
*****************************
when im telnetting to it, it returns
*** both talking *** (just once, expected was twice)
and
*** parent talking ***
as expected, and the server itself prints 'client got' and all the
other stuff i wanted it to print.
but the 'child talking' and 'Hi & goodbye!' dont go to the client.
So my question is: what am i doing wrong? As far as I can see,
-> print CLIENT "\n\r*** parent talking ***\n\t"; <-
works well for the parent but not for the child.
Any help welcome ;-)
.
- Follow-Ups:
- Re: millionth socket problem....
- From: Jim Gibson
- Re: millionth socket problem....
- Prev by Date: Re: Want to add exception for a auto run sub in module
- Next by Date: file test
- Previous by thread: perl-java interface...
- Next by thread: Re: millionth socket problem....
- Index(es):
Relevant Pages
|