in reply to Re: Socket Traffic Cop
in thread Socket Traffic Cop

That is an astute observation. In fact I tried that, but the said 'right piece of code' works correctly only with one connection, when I hand that same code the socket connection from the multi connection version, it doesn't work correctly.
I think I will have to use an F5 appliance to do this unfortunately. Thanks

Replies are listed 'Best First'.
Re^3: Socket Traffic Cop
by BrowserUk (Patriarch) on Apr 13, 2010 at 03:10 UTC

    Hm I have no idea what an "F5 appliance" is. But, I'd love to understand what you mean by:

    but the said 'right piece of code' works correctly only with one connection, when I hand that same code the socket connection from the multi connection version, it doesn't work correctly.

    The socket returned from accept, will only be connected to one client at any given time. It could come from any of the five client machines. Discovering which machine (ip address) it comes from is easy. So routing each incoming client connection to a thread that is dedicated to dealing with that connection should be very easy.

    Ostensibly:

    sub client1 { my $fno = shift; open my $client, '+<&', $fno or die; while( <$client> { if( /... / { print $client ...; } elsif( /.../ ) { print $client ...; else { print $client ...; } } } sub client2 { ... } sub client3 { ... } sub client4 { ... } sub client5 { ... } my %lookup = ( 'xxx.xxx.xxx.xxx' => \&client1, 'yyy.yyy.yyy.yyy' => \&client2, ... ); my $lsn = IO::Socket::INET->new( Listen => 5, LocalPort => $port ); while( my $client = $lst->accept ) { my $addr = $client->peerhost; thread->create( $lookup{ $addr }, fileno( $client ) )->detach; }

    And that's pretty much it. Each client (type/machine/whatever) gets a dedicate thread that talks to it and only it. That thread runs a simple conversation dedicated to that clients needs. Even concurrent clients from a given machine get a thread dedicated to just that conversation.

    No complicated, state machine mixing concerns of listening for clients; accepting input and sending output all together in a spaghetti nightmare of global variables and transient states. But, people seem to like complicated.

    POE is extraordinarily clever code. Trouble is, it seems to require extraordinary programmers to use it. And if one them has coded a wheel that does exactly what you need to do, and you can find it, and understand the documentation, then you can hire it and you're set to go.

    But need something slightly different to what's available and you're in the lap of the gods awaiting yet another distribution tailored to your exact needs.


    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.
      Thanks, I'll give this code a try, once I can get POE installed. An F5 is a fancy DNS/Port hardware device, which probably runs on linux....load balancing or other things.

        The "code" I posted is only pseudo-code. It uses threads, not POE. It doesn't require, and would not work, in conjunction with POE.

        I assumed from the code you posted above that you were already using POE. And I was suggesting that it might be easier to not do so.


        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.