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 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();
--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-)
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |