I posted earlier here about problems I was having with socket programming.

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:

#!/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;
Here's the server code:
#!/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; }

In reply to Bi-directional communication between 2 clients and 1 server over a single socket by Magius_AR

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.