Personally , I would use pipes, but since that's not an option on this extra credit homework assignment, I'll give you something more tangible. As I understood it, you need to handle BOTH clients at the same time, so the above will not work. You will need to save the returned accepted connections to separate variables. Here's the code I promised in the CBox. You really should be using IO::Socket::Unix but its not really a big deal for this homework. A better solution would be perform a UNIX select(2) but since the operations performed are so simple and fast the processes are all in the same running group, I have excluded this. Also, I do not see a proper bind, but since I am unfamiliar with IO::Socket, perhaps this is my mistake.
--initialization as you have it-- $cli1=$server->accept(); $cli1->recv($data,10,0); die 'Aycaramba!' if $data ne 'READY' ; $cli2=$server->accept(); $cli2->recv($data,10,0) die 'Aycaramba again, mon!' if $data ne 'READY'; #$maxgames is predefined (passed from the exec!) #$cli1score #$cli2score for(1..$maxgames) { $cli1->print('GO'); $cli2->print('GO'); #ready to rock 'n' roll! #just wait for the input from the first player $cli1->recv($cli1var,10,0); $cli2->recv($cli2var,10,0); #now play the game my %win=('Rock',1,'Paper',2,'Scissors',3); my $avar1,$avar2; #warning: very little error-handling, but do you need it? $avar1=$win{$cli1var}; $avar2=$win{$cli2var}; if($avar1==$avar2){} elsif($avar1-1 == $avar2){$cli1score++;} elsif($avar1==1 && $avar2==3){$cli1score++;} else{cli2score++;} } #end long game loop $cli1->print('STOP'); $cli2->print('STOP'); print "total scores: player 1: $cli1score, player 2: $cli2score, playe +r user: EXTRA CREDIT POINTS!\nHave a nice day\n"; $cli1->close(); $cli2->close();
This is untested, but logically functional. This is the way you want to head in your program. The client is even simpler. All it does is connect, block on a read in an infinite loop for 'GO' and pick a random string of the three to send. Thus, the little doc's comment about the string passing challenge is crushed by the superiority of Perl!!!! Hahaha! Anyway, try to fork() a client like this:
--initialization stuff as you have it-- #$serv is the opened handle to the server my %value=(1,'Rock',2,'Scissors',3,'Paper'); while(1) { $serv->recv($data,10,0); if($data eq 'STOP') die "server asked for stop\n"; $serv->print($value{int(rand(4)}); }

I left you kind of a mess with undeclared vars and stuff but the core functionality is there.

dude, that's it! go for the gold and good luck. if you can slip it in, name one of the variables after me! 8-)

AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.

In reply to Re: Bi-directional communication between 2 clients and 1 server over a single socket by AgentM
in thread 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.