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

I'm trying to make one script open two client sockets to two different servers and read/write to both servers as conditions require. Is this even possible? If it is, is there a resource or module that anyone can direct me to that would assist me?

Since I've been having such a difficult time finding a lead on how to exactly do this, I suspect that I'll need two scripts each maintaining a client connection and sharing results to each other (or a third script) via shared memory.

Thank you for any assistance you may give.
  • Comment on Single non-server script maintaining multiple client connections

Replies are listed 'Best First'.
Re: Single non-server script maintaining multiple client connections
by Fletch (Bishop) on Feb 03, 2008 at 23:55 UTC

    Why would it not be possible? "Server" and "client" aren't magic words, they're descriptions of roles.

    Once you're over the learning curve POE would make something like this trivial, but there's nothing preventing you from doing something like this using select (or IO::Select) directly yourself either.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Single non-server script maintaining multiple client connections
by ysth (Canon) on Feb 04, 2008 at 02:13 UTC
    How would a script to talk to multiple servers be organized? Can you describe it? (I ask to get an idea of which part you think may not be possible; it's really not clear to me.) Maybe it's select that you are missing?
Re: Single non-server script maintaining multiple client connections
by BrowserUk (Patriarch) on Feb 04, 2008 at 04:02 UTC

    There are many ways to do this. Which is most appropriate really depends upon the nature of the logic involved.

    For example, if the conversations with each of the servers is complex and mostly independant of the other, with only the occasional need to share state, then putting each in it's own thread and sharing state through a queue or shared variables may be the simplest approach.

    If however, the conversations are highly interdependant with each driving the other, then the select approach makes more sense.

    Each approach has both advantages and caveats. The right choice depends entirely upon the nature of the application.


    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: Single non-server script maintaining multiple client connections
by pc88mxer (Vicar) on Feb 04, 2008 at 05:03 UTC
    For a good thorough treatment of socket and multiplexed IO programming, I'd recommend Lincoln Stein's book Network Programming with Perl.

    The "as conditions require" part of your problem is implemented by the select function call. The difficulty with select-based approaches is that all of you logic is "turned inside-out". Modules like POE or even using threads make it easier to write your application logic at a small performance penalty (and learning curve to learn how to use POE.) On the other hand, it is helpful to learn how to multiplex I/O using select so that you have a better understanding of what is really going on when using something like POE.