simonz has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I have written a client-server program, where the client continuously sending some message after an interval. But now
I want this to be done in a 2-threaded mechanism, where every new connection should start a new thread to accept the data.
The main thread will always accept the new connection and every thread will try to append to a shared variable say $shared_data. And the main thread will attempt to read from that shared_data from the beginning of it.
On the other hand every client should randomly send some data of some particular pattern (eg. delimited by a string 'EOM'). Every thread getting this info should write it to
the shared variable everytime when 'EOM' arrives. The main thread will look for the 'EOM' in the shared data and remove it till then by locking and unlocking the shared data.
I need some help on programming this with thread. How in the server code , I will introduce the following threading mechanism to accomplish this
Below is my client server code$thr = threads->create(\&worker_thread);
# client #use IO::Socket; use IO::Select; use strict; my $server = IO::Socket::INET::->new(Proto => 'tcp', LocalPort => 55555, Listen => 1, Reuse => 1 ) or die "Server can't start: $!" +; my $readable_handles = new IO::Select(); $readable_handles->add($server); my $buf; while (1) { # select() blocks until a socket is ready to be read or written my ($new_readable) = IO::Select->select($readable_handles, undef, undef, 0); # If it comes here, there is at least one handle # to read from or write to. For the moment, worry only about # the read side. foreach my $sock (@$new_readable) { print "Inside foreach $sock \n"; if ($sock == $server) { my $new_sock = $sock->accept(); # Add it to the list, and go back to select because the # new socket may not be readable yet. $readable_handles->add($new_sock); } #- server part else { #print STDERR "Reading...\n"; # It is an ordinary client socket, ready for reading. $buf = <$sock>; if ($buf) { #- print the buffer print "Read $buf\n"; # .... Do stuff with $buf } else { # Client closed socket. We do the same here, and remove # it from the readable_handles list $readable_handles->remove($sock); close($sock); } } } }
use IO::Socket; my $client = IO::Socket::INET::->new( Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 55555 ) or die "Client can't connect: $!"; my @msgs = 1 .. 100; for (@msgs) { print $client "$_\n"; sleep 1; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to introduce threading in socket communication
by BrowserUk (Patriarch) on Oct 03, 2013 at 14:22 UTC | |
by Anonymous Monk on Oct 03, 2013 at 14:35 UTC | |
by BrowserUk (Patriarch) on Oct 03, 2013 at 15:00 UTC | |
by simonz (Sexton) on Oct 03, 2013 at 17:19 UTC | |
by BrowserUk (Patriarch) on Oct 03, 2013 at 18:40 UTC | |
Re: How to introduce threading in socket communication
by golux (Chaplain) on Oct 13, 2013 at 16:22 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2013 at 17:11 UTC |