Click here to get back home

2 clients 2 servers with threads

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
2 clients 2 servers with threads tobyboy 10-28-2008
Get Chitika Premium
Posted by tobyboy on October 28, 2008, 9:04 am
Please log in for more thread options
Hello,

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!

Cheers,
Toby


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
threads->new(\&readClient,$client1,"CLIENT1")->detach;
printf "T1 STARTED\n";
threads->new(\&readClient,$client2,"CLIENT2")->detach;
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;
}

Posted by xhoster on October 28, 2008, 6:28 pm
Please log in for more thread options
tobyboy@astroboymail.com wrote:
> Hello,
>
> 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.

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

...
> my $client1 = setupClient($port_client1);
> my $client2 = setupClient($port_client2);
...

> sub readClient {
> my $client = shift;
*^*
> my $name = shift;
> while (1)
> {
> read ($client1, my $buf, 1);
*^*

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.

Xho

--
-------------------- 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.

Similar ThreadsPosted
how to read email automatically without POP3 and IMAP servers in perl? December 13, 2004, 1:06 pm
ImageMagick and threads October 12, 2004, 1:44 pm
perl and threads July 18, 2006, 9:20 am
LWP::UserAgent + HTTPS + threads ==> segmentation fault November 11, 2004, 9:57 pm
xs module, threads and clone of returned objects January 12, 2008, 6:27 am
threads, XSUB allocated memory, destructors, destruction September 30, 2005, 6:31 pm
SOAP::Lite POD::WSDL and .NET clients April 25, 2007, 12:41 pm
multiple clients one server - proxy scenario October 28, 2008, 7:02 am
xs module, threads and clone() - how does clone work ? January 8, 2008, 3:42 am
DBD::Oracle and various oracle clients August 1, 2006, 9:23 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap