Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

Is it possible to write a client program which connects to 2 servers and handles it. I'm asking about multiplexing in client program?

Replies are listed 'Best First'.
Re: Multiplexing in client
by BrowserUk (Patriarch) on Apr 22, 2010 at 11:38 UTC

    Yes. This will send/receive messages continuously, to each of two echo servers concurrently:

    #! perl -slw use strict; use threads; use IO::Socket; my $t1 = async { my $s = IO::Socket::INET->new( 'localhost:12345' ); for ( 1 .. 1e6 ) { printf $s "Message %6d\n", $_; printf 'Sent and received: %s', scalar <$s>; sleep 1; } }; my $t2 = async { my $s = IO::Socket::INET->new( 'localhost:54321' ); for ( 1 .. 1e6 ) { printf $s "Message %6d\n", $_; printf 'Sent and received: %s', scalar <$s>; sleep 1; } }; $_->join for $t1, $t2;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Multiplexing in client
by zwon (Abbot) on Apr 22, 2010 at 11:26 UTC