in reply to Socket Traffic Cop

is it feasible to have a perl socket listener on one single tcp port that then redirects the socket feed after examining the ip address to another listener on the same box ?

It doesn't make much sense to do that at the level of a Perl script. Once the remote client connects, it won't, in the absence of some pre-existing protocol, reconnect to another listener. All you could do is have your server connect to the other local listeners and then act as go-between (proxy), forwarding each inbound packet and getting any responses and sending them back to the remote system.

It won't make any future connects (seperate or concurrent) from that remote system go directly to the other local listener. They'll still come through the original listener. Each having to connect to a new instance of the other local listener and relay through. You'd just be creating lots of middleman processes that do nothing but create a bottleneck.

Once a remote client connects to your listener, all you need is to hand off the accepted socket to the right piece of code--be that a process or thread--dependent upon the remote IP of the inbound connection. And that's very easy, far less costly, and far less fragile.


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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Socket Traffic Cop
by sans-clue (Beadle) on Apr 13, 2010 at 02:13 UTC
    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

      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.