otijim has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks...

I am trying to write up a Client/Server Script using IO::Socket::INET and I continue to get strange results.

I am using a fork to allow the server to serve multiple clients.

It seemed everything was working, but now it seems to be hanging just after the fork call.

The first client connects and everything is fine. Then the second client connects, the program tries to fork and the stops. As soon as the first client disconnects then the second client continues normally.

I don't believe the problem is related to the FORK, it seems to be related to reading from the sockets.

I generally read and write to the sockets as such:
print $socket "Hi there\n";
my $msg = <$socket>;

If anyone has some experience with this please help. I've gone through examples and docs I could find on the web, but they don't seem to work very well either :)

OS Info: Window 2000 sp2
Perl Info:v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail)
Binary build 626 provided by ActiveState Tool Corp.
Built 01:31:15 May 2 2001

Replies are listed 'Best First'.
Re: TCP IO:Sockets problem
by MZSanford (Curate) on Jul 13, 2001 at 21:20 UTC
    I have a box with a similar setup, and find that hangning until the first client disconnects usually comes from the placment of my accept. By this i mean that even though i call fork, i end up queing connections ... no good way to describe it short of code ... but rather than add code to this, would you mind posting the code you are currently using, even just a stripped down version ? this way, i am not writting code, but rather helping you to find the bug so you can release the übercode inside.
    OH, a sarcasm detector, that’s really useful
Re: TCP IO:Sockets problem
by otijim (Acolyte) on Jul 13, 2001 at 21:36 UTC
    Okay...Here is some code.....I in this version it won't connect the second until the first is done reading....I understand that the <$socket> is 'blocking' but does that effect children and forks??

    use IO::Socket::INET; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; my $socket = IO::Socket::INET->new( LocalPort => 1776, Type => SOCK_STREAM, Reuse => 1, Listen => 10 ) or die "Big Problem with the Server, Man : $!\n\n"; while ( my $client = $socket->accept() ) { my $child; # perform the fork or exit die "Can't fork: $!" unless defined ($child = fork()); if ($child == 0) { #i'm the child! #close the child's listen socket, we dont need it. $socket->close; #call the main child rountine print $client 'Hello, down there!!', "\n\n"; $msg = <$client>; ### ** When one child is here the others seem t +o hang until its done ** #if the child returns, then just exit exit 0; } else { #i'm the parent! #who connected? warn "Connecton recieved ... ",$client->peerhost," ... Sent to Chi +ld: $child\n"; #close the connection, the parent has already passed # it off to a child. warn "Waiting for Child to start...\n"; sleep(1); # Give the child a moment to start warn "Closing Parent connection.\n\n"; $client->close(); } } close($socket); exit 0;
      I verified that this doesn't work under ActiveState. I also ran this code on Linux and CYGWIN and it works just fine. Looking at TaskManger under 2000, I only see 1 Perl.exe process regardless of how many clients are connected. So it looks like it's ActiveState's fork emulation. Sorry dude.
Re: TCP IO:Sockets problem
by bschmer (Friar) on Jul 13, 2001 at 21:21 UTC
    I've done a bunch of this kind of thing lately (not on ActiveState Perl, mind you) so it may help if you could let us see the relevant code.