Reputation: 3131
I have two Perl scripts that are supposed to do the following job:
Server starts and wait for the client to connect. The client connects and istantly send an XML file. Server reads XML file and starts a couple of threads. Each thread receive $client socket as a parameter and once they're done they have to send the output back to the client. Meanwhile the client is waiting for messages to arrive, he's supposed to print those messages on the command line.
Now my problem is that I don't know how to implement the last part. I tryed to use the receive method but both programs get stuck if I do that.
Server side:
my @threads;
$server = IO::Socket::INET->new(
Listen => 1,
LocalAddr => 'localhost',
LocalPort => '8080',
Proto => 'tcp'
) or die "Can't crete socket!\n";
$client = $server->accept;
open FILE, ">".$filename or die "Can't open file!\n";
while (<$client>) {
print FILE $_;
}
close FILE;
# READ FILE AND DO SOME OTHER STUFFS
push @threads, threads ->create(\&subroutine1, $parameters, $client);
# OTHER STUFFS
push @threads, threads ->create(\&subroutine2, $parameters, $client);
# MORE THREADS...
sub subroutine1 {
my @pars= @_;
my $parameters = $pars[0];
my $client = $pars[1];
my $sub1 = `perl other_script.pl $parameters`;
$client->send($mem);
}
Client side:
my $server = IO::Socket::INET->new(
PeerAddr => 'localhost',
PeerPort => '8080',
Proto => 'tcp'
) or die "Can't create client socket!\n";
open FILE, $filename;
while (<FILE>) {
print $server $_;
}
close FILE;
# HOW DO I READ INFORMATIONS?
$server->close();
Upvotes: 2
Views: 894
Reputation: 39158
You cannot put the send part into the children/worker threads, it'll be jumbled.
Q: Why did the multi-threaded chicken cross the road?
A: other to side. To the get
Instead, have the parent/boss thread collect the results of the children, sort the data if necessary, serialise, then send as a whole.
You read with the recv
function, also available as a method in IO::Socket
.
Upvotes: 2