Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
One program is the main program, which fork/execs one referee process and two player processes. It then waits until all three terminate. It should check that the command line parameter specifies the number of games is legal and pass it to the referee process as a parameter to exec().
One program is a referee program, which playes the role of a server. This program should prepare a socket and then listen to both players to send the string ``READY'', which means that they're ready to make a choice. It should then tell each player to make a choice by sending them both the string ``GO''. Their responses are then read, and their scores are calculated and updated. This process should be repeated until all of the turns have been taken, at which point the referee should send both players the string ``STOP'', which causes them to terminate.
One program is a a player program, which playes the role of the client. This program is executed twice by the main program, and should start by connecting to the referee's socket. It should then send the ``READY'' message. When it receives the ``GO'' message back from the referee, it should make a choice anbd send it as a stringto the referee. When it receives the string ``STOP'', it should kill itself.
Here's what I have so far:
Here's the main program "play":
Here's the referee "server":#!/usr/bin/perl die "Invalid number of games" unless ($ARGV[0] > 0); system "./server $ARGV[0] &"; sleep 1; $pid = fork(); die "unable to fork: $!" unless defined $pid; exec "./client &";
Here's the player "client":#!/usr/bin/perl use IO::Socket; $server = IO::Socket::INET->new( Proto => 'tcp', LocalHost => 'localhost', LocalPort => '6969', Listen => 1, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n"; while ($client = $server->accept()) { while (<$client>) { if ($play_game) { #play game in here } elsif (/READY/i) { $players_ready++; } if ($players_ready == 2) { print $client "GO\n"; $play_game = +1; } } close $client; }
My problem is in getting the server program to communicate with the client.#!/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 $0 connected to server]\n"; print $client "READY\n"; while ($server = $client->accept()) { while (<$server>) { if (/GO/i) { srand; # seed the random number function $num = int (rand 4); # get a random integer, 1 through 104 if ($num == 1) { print $server "Rock\n"; } elsif ($num == 2) { print $server "Paper\n"; } elsif ($num == 3) { print $server "Scissors\n"; } } if (/STOP/i) { last; } } close $server; }
Here's what I know:
When I run "play", the server receives the 2 "READY" signals from the clients and the following code line DOES get executed:
However, the if (/GO/i) conditional in the client program is not getting executed and I do not know why. Is it something simple or is bi-directional communication really harder than I originally thought?if ($players_ready == 2) { print $client "GO\n"; $play_game = 1; }
My only guess is that somehow I'm not sending the "GO" message properly. I just don't understand why the server was able to receive the "READY" with no problem, but the client is choking on the "GO". Any help would be much appreciated.
Magius_AR
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Socket programming
by Jonathan (Curate) on Oct 06, 2000 at 14:52 UTC | |
by Zarathustra (Beadle) on Oct 07, 2000 at 05:17 UTC | |
|
RE: Socket programming
by Magius_AR (Sexton) on Oct 06, 2000 at 13:40 UTC | |
|
RE: Socket programming
by Zarathustra (Beadle) on Oct 07, 2000 at 04:58 UTC | |
|
Re: Socket programming
by AgentM (Curate) on Oct 06, 2000 at 23:17 UTC |