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