Coruscate has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Non-forked, bi-directional client/server
by jepri (Parson) on Jan 20, 2003 at 06:18 UTC | |
|
•Re: Non-forked, bi-directional client/server
by merlyn (Sage) on Jan 20, 2003 at 05:59 UTC | |
|
Re: Non-forked, bi-directional client/server
by Corion (Patriarch) on Jan 20, 2003 at 08:31 UTC | |
|
Re: Non-forked, bi-directional client/server
by castaway (Parson) on Jan 20, 2003 at 09:53 UTC | |
|
Re: Non-forked, bi-directional client/server
by Veachian64 (Scribe) on Jan 20, 2003 at 15:28 UTC | |
|
Re: Non-forked, bi-directional client/server
by pg (Canon) on Jan 20, 2003 at 15:58 UTC | |
|
Re: Non-forked, bi-directional client/server
by Excalibor (Pilgrim) on Jan 21, 2003 at 11:01 UTC |