I'm trying to write this bidirectional client/server communication application, it simulates a very basic chat example, the program works in part, it is supposed to asynchronously take input from either the server or the client and then show that input at the opposite end.. for now, it only takes input from the client and show it at the server session but can not accept anything from the server, It's not possible to type in at the server console the same way I do in the client one...

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); <>;

#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); <>;
Thanking you for input and advice...

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:

if(defined($child_pid)){} if($child_pid){} if($child_pid==1){} if($child_pid==0){}
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 ...


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

In reply to Bidirectional Communication using sockets and forking by biohisham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.