In the past, there has been a lot of discussion on the site about client/server applications written in perl. For the most part, the examples presented on PM use fork() to pass client connections to a child process so that the parent can continue to accept new connections (for multiple clients handled at the same time, not one after another).

So now it's my turn to ask some questions about client/server programming in perl. Here's what I am trying to accomplish:

Thanks to This post, I now have the following code:

# server.pl use strict; use IO::Socket; use IO::Select; my $main_sock = new IO::Socket::INET( LocalAddr => '127.0.0.1', LocalPort => 1200, Proto => 'tcp', Listen => 1, ReuseAddr => 1, ); die "Could not initialize server: $!\n" unless $main_sock; print "Server has successfully initialized.\n\n"; my $r_handles = new IO::Select(); $r_handles->add( $main_sock ); while (1) { my ($new_r) = IO::Select->select( $r_handles, undef, undef, 0 ); foreach my $sock (@$new_r) { if ($sock == $main_sock) { my $new_sock = $sock->accept(); $r_handles->add($new_sock); } else { my $buf = <$sock>; if ($buf) { print "$buf\n"; } else { $r_handles->remove($sock); close $sock; } } } } # client.pl use strict; use IO::Socket; my $client_id = $ARGV[0]; my $sock = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => 1200, Proto => 'tcp', ); die "Could not connect to server: $!\n" unless $sock; for (1 .. 5) { print $sock "From $client_id: Msg $_\n"; sleep 1; } close $sock;

Okay, so there is no fork() here and it successfully allows multiple clients to be connected at any given time. The problem I've run into is allowing data to flow both ways on the connection. I do know a little bit about sockets (though not nearly as much as I'd like) and know that I will most likely have to setup the second communication line on another port. This would be fine. So what is my guess as to what I would do? More or less mix the two scripts together so that I have one listener and one sender on each side of the connection. The part I just cannot figure out for the life of me is where I would/could/should mix the scripts together. The current server (that receives data from the client) is in an infinite loop, therefore I'd have to send data somewhere within that loop. But where and exactly how? I also understand that if I do end up having 2 connections in each script that I need the IP address for both sides to get things going. But don't worry, I already have a sneeky way to do that.

I have now confused myself to death over this, and I'd really love to discover a solution to this. I've wanted a bi-directional client/server app for a while, but not until today did I find those examples that do not require the use of fork(). Of course, if WIn32 properly supported fork, I would not have to ask this, but you know how things are :) Help!


In reply to Non-forked, bi-directional client/server by Coruscate

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.