Rudolf has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I have a forked process here with one child, each process runs its own while loop. The parents job is to read STDIN then send it to the server; while the job of the child is to print any recieved messages from the server even while the parent is waiting for stdin. I assumed this would work but it seems as soon as the child attempts to recv() from the server to check for messages - the parent stops and the tcp connection appears broken. Im sure I do not fully understand the inner workings of how perl deals with the tcp connection so I might be making my mistake there, thank you
use v5.14; use Socket; $| = 1; my $user_name = 'default'; socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp')); my $iaddr = inet_aton('localhost'); my $packed_addr = sockaddr_in(5555, $iaddr); connect(SERVER,$packed_addr) or die "Cant connect: $!\n"; print STDERR "[Connected to server as \"$user_name\"]; \tfor a list + of commands type /help\n"; send(SERVER,$user_name,1); if(my $pid = fork){ while (1){ print "Send: "; chomp(my $data = <STDIN>); send(SERVER,$data,1); } } else{ while(1){ if( recv(SERVER,my $info,1024,0) ){ say $info; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: recv() on forked process
by BrowserUk (Patriarch) on Jul 26, 2012 at 20:20 UTC | |
by zentara (Cardinal) on Jul 27, 2012 at 13:17 UTC |