biohisham has asked for the wisdom of the Perl Monks concerning the following question:
I basically wanted to fork each program into a child and parent, the parent process in the client accepts user input through STDIN, while the client's child process listens to the socket and takes input from the server...on the server side, the parent process would take input from the client whereas the child process is supposed to read STDIN from the console run by the server session (this is not taking place). For the past 2 hours I can not fathom where I am getting stuck, I went through the IO::Socket::INET and other suggested documentations, but it seems there's a trivial thing that I can't point a finger on and hence I am resorting to the Monks' help since I am yet laying my very early foundations on net programming.
#Server.pl #!/usr/local/bin/perl use strict; use warnings; use IO::Socket; my $server = IO::Socket::INET->new( LocalPort=> 1121, Type=>SOCK_STREAM, Reuse=>1, Listen=>1 )or die ("Could not create server"); while(my $client=$server->accept()){ my $child_pid; unless(defined ($child_pid=fork())){die"can not fork\n";} if(defined($child_pid)){ while(my $line = <$client>){ print "CLIENT says:$line\n"; } }else{ while(my $line = <>){ print $client "$line\n"; } } } close($server); <>;
Thanking you for input and advice...#Client.pl use strict; use warnings; use IO::Socket; my $socket = IO::Socket::INET->new( PeerAddr =>"192.168.1.100", PeerPort =>1121, Proto =>"tcp", Type =>SOCK_STREAM, )or die ("Could not create client"); #fork the process my $child_pid; unless (defined($child_pid=fork())){die("can not fork")}; if(defined($child_pid)){ while(my $line = <STDIN>){ print $socket $line; } }else{ while(my $line = <$socket>){ print "SERVER says: $line\n"; } } close($socket); <>;
update1: The system I run this on is WinXP with Perl v5.10, the same system plays server and client at the same time..unfortunately, I tried different combinations of:
on both server and client, the farthest I got it to half work is in the context posted above, I am reading about sockets once again in hopes I find clues but I am yet in need of assistance ...if(defined($child_pid)){} if($child_pid){} if($child_pid==1){} if($child_pid==0){}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bidirectional Communication using sockets and forking
by almut (Canon) on Dec 28, 2009 at 12:30 UTC | |
by Anonymous Monk on Dec 28, 2009 at 12:46 UTC | |
by almut (Canon) on Dec 28, 2009 at 13:06 UTC | |
|
Re: Bidirectional Communication using sockets and forking
by zwon (Abbot) on Dec 28, 2009 at 12:46 UTC | |
|
Re: Bidirectional Communication using sockets and forking
by zentara (Cardinal) on Dec 28, 2009 at 13:11 UTC | |
|
Re: Bidirectional Communication using sockets and forking
by zentara (Cardinal) on Dec 28, 2009 at 16:33 UTC | |
|
Re: Bidirectional Communication using sockets and forking
by afoken (Chancellor) on Dec 29, 2009 at 13:57 UTC |