Re: millionth socket problem....
- From: "Jim Gibson" <Jim@xxxxxxxxxx>
- Date: 23 Jan 2006 16:52:46 -0800
..rhavin grobert wrote:
> 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 ;-)
Your program worked for me, if I changed the port number to 7023, as
ports below 1024 are reserved by the system, and I telnet from the same
host using 'localhost' as a host name. You should only get "***both
talking *** once, because your program sends that to the client before
forking.
.
- Follow-Ups:
- Re: millionth socket problem....
- From: .rhavin grobert
- Re: millionth socket problem....
- From: .rhavin grobert
- Re: millionth socket problem....
- References:
- millionth socket problem....
- From: .rhavin grobert
- millionth socket problem....
- Prev by Date: Re: Forking and etc.
- Next by Date: Re: file test
- Previous by thread: millionth socket problem....
- Next by thread: Re: millionth socket problem....
- Index(es):
Relevant Pages
|