2 clients 2 servers with threads

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I would like to implement a Perl script which receives data from 2
clients via a socket connection and prints the result to standard out.

My idea would be to establish 2 connection with 2 servers one after
the other (I do this for example with Telnet console) then create the
reading threats both doing a while(1)-loop and wait in the program in
a while(1)-loop. I experience 2 problems:

   1) second argument of readClient is the client name which would
tell me which client prints data. even though
       stdout prints "CLIENT1" and "CLIENT2" data was only send from
the first client

   2) I can only send data from the first client. data send form the
second client seems never to reach the
      corresponding server

I attached the code. For any help I am very gladful!


use strict;
use threads;

my $port_client1 = 2540;
my $port_client2 = 2550;
use IO::Socket;
use IO::Select;

# setup the clients
my $client1 = setupClient($port_client1);
my $client2 = setupClient($port_client2);

# start the threads
printf "T1 STARTED\n";
printf "T2 STARTED\n";
while (1) { }

# read one character from client and print to stdout
sub readClient {
    my $client = shift;
    my $name   = shift;
    while (1)
       read ($client1, my $buf, 1);
       printf "$name: $buf\n";

# create client connection
sub setupClient {
    my $port = shift;
  # create server1 and server2
  my $server = IO::Socket::INET->new(
      LocalPort => $port,
      Type => SOCK_STREAM,
      Reuse => 1,
      Listen => 1
  or die "Couldn't be a tcp server on port $port : $@\n";
  my $client = $server->accept();
  return $client;

Re: 2 clients 2 servers with threads

tobyboy@astroboymail.com wrote:
Quoted text here. Click to load it

Which one would you prefer help on, this one or the other thread you
started on a similar topic?

Quoted text here. Click to load it

Quoted text here. Click to load it

You are reading from $client1, a handle inherited from
the outer scope.  So of course you don't find the stuff from
$client2.  You should read from $client.


-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Site Timeline