Magius_AR has asked for the wisdom of the Perl Monks concerning the following question:
Well I'm having a few more :)
I can't get my server to accept multiple (namely 2) clients. It accepts the first client fine and prints out the appropriate ready message, but doesn't even try to allow for the second one to connect.
Do I need to use IO::Select somehow? I've contacted several sources (including perl.com and google) trying to figure out how to allow a server to deal with multiple clients and most of um seem to point to Select.
My code is posted below. Any help would be much appreciated. I've primarily trying to get the server to accept 2 clients as handles, then communicate bi-directionally between each client and the server.
Here's the client code:
Here's the server code:#!/usr/bin/perl -w use IO::Socket; # create a tcp connection to the specified host and port $client = IO::Socket::INET->new( Proto => "tcp", PeerAddr => 'localhost', PeerPort => '6969' ), or die "can't connect to server: $!"; print $client "READY\n"; while (<$client>) { if (/GO/i) { srand; # seed the random number function $num = int (rand 3); # get a random integer, 0 through 2 if ($num == 0) { print $client "Rock\n"; } elsif ($num == 1) { print $client "Paper\n"; } elsif ($num == 2) { print $client "Scissors\n"; } } if (/STOP/i) { last; } } close $client;
#!/usr/bin/perl use IO::Socket; $server = IO::Socket::INET->new( Proto => 'tcp', LocalHost => 'localhost', LocalPort => '6969', Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n"; print "Rock, Paper, Scissors: $ARGV[0] iterations\n\n"; while ($client = $server->accept()) { print "[Server $0 receiving client $client]\n"; while (<$client>) { if ($play_game) { #play game in here } elsif (/READY/i) { $players{$client} = ++$players_ready; print "Player $players{$client}: Ready\n"; } if ($players_ready == 2) { print $client "GO\n"; $play_game = +1; } } close $client; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bi-directional communication between 2 clients and 1 server over a single socket
by AgentM (Curate) on Oct 10, 2000 at 08:53 UTC | |
by johannz (Hermit) on Oct 10, 2000 at 21:12 UTC | |
by AgentM (Curate) on Oct 10, 2000 at 22:27 UTC | |
Re: Bi-directional communication between 2 clients and 1 server over a single socket
by merlyn (Sage) on Oct 10, 2000 at 07:08 UTC | |
by Anonymous Monk on Oct 10, 2000 at 07:58 UTC |