Trying to code the following:

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":

#!/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 referee "server":
#!/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; }
Here's the player "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; }
My problem is in getting the server program to communicate with the client.

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:

if ($players_ready == 2) { print $client "GO\n"; $play_game = 1; }
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?

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


In reply to Socket programming by Anonymous Monk

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.