I precede this by saying that I have minimal programming experience in both Perl and Socket, so I appreciate any help I can get. I have a TCP server that needs to simultaneously process several client connections and be able to receive data from any of the Clients at any time, as well as be able to send data back to Clients based on the information it received. For example, Client1 and Client2 connect to my server. Client2 sends "Ready", the server interprets this and sends "Go" to Client1. Here is what I have written so far:
my $sock = new IO::Socket::INET
{
LocalHost => $host, // defined earlier in code
LocalPort => $port, // defined earlier in code
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1,
};
die "Could not create socket $!\n" unless $sock;
while ( my ($new_sock,$c_addr) = $sock->accept() ) {
my ($client_port, $c_ip) = sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host = "";
my @threads;
print "got a connection from $client_host", "[$client_ipnum]\n";
my $command;
my $data;
while ($data = <$new_sock>) {
push @threads, async \&Execute, $data;
}
}
sub Execute {
my ($command) = @_;
// if($command) = "test"
// send "go" to socket1
print "Executing command: $command\n";
system($command);
}
, while , accept , , .