How to handle client server communication in Perl?

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();

Answers


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.


Need Your Help

Good Design for Initialization of Static Array

c++ api design

I have a question regarding good design in C++. I have a class A, and all objects of this class use an integer array of constant values (they should share the same array, as their values are const...

Using C#, how do I set tab positions in a multiline textbox?

c# winforms tabs textbox multiline

Is there a graceful way to set custom tab sizes/positions in a multiline textbox in C#?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.