Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Simple proxy: Net::Tcp::Server vs. IO::Connect

by dd-b (Monk)
on Sep 16, 2011 at 15:17 UTC ( [id://926430]=perlquestion: print w/replies, xml ) Need Help??

dd-b has asked for the wisdom of the Perl Monks concerning the following question:

I need to write a simple proxy + loadbalancer (NOT an http proxy) that will accept a connection and the first line of protocol, pick an actual server based on info in that first line plus data I have on server loads, and fork off a child that then channels everything from the client through itself to the selected server/port (and vice versa).

(The routing depends on data in the first line of protocol received from the client, so I can't just use a generic load-balancer; a large part of the point is hiding the splitting up of some overloaded servers from the client software.)

It looks like I can do this fairly easily using IO::Connect (all the pieces, how to set the backlog queue size, how to do non-blocking IO all seem pretty obvious).

Liking, in principle, using higher-level tools, I started looking at Net::TCP and sub-classes including Server. While I can create a server, accept a connection, and interact with that connection, I can't actually find how to do all the little adjustments to params, or how to do simple stuff like determining the IP address of the client that connected to me.

So, my question is -- for a really simple proxy like this, is there actually an advantage to me in using Net::TCP::Server, or is going straight to IO::Connect the right choice? And if I *should* be using n::t::s, is there perhaps a tutorial or HOWTO somewhere that would get me started better than just reading the man pages has?

  • Comment on Simple proxy: Net::Tcp::Server vs. IO::Connect

Replies are listed 'Best First'.
Re: Simple proxy: Net::Tcp::Server vs. IO::Connect
by zentara (Archbishop) on Sep 16, 2011 at 17:52 UTC
    or is going straight to IO::Connect the right choice?

    I couldn't find IO::Connect on CPAN, do you mean IO::Socket? There are plenty of code examples on google for IO::Socket, search for perl IO::Socket proxy


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Yeah, for some reason I've been trying to rename that module all day.
Re: Simple proxy: Net::Tcp::Server vs. IO::Connect
by dd-b (Monk) on Sep 16, 2011 at 23:21 UTC

    Okay, so I'm playing with IO::Socket, since nobody has tried to convince me of the value of higher-level facilities and the examples largely use IO::Socket.

    I can listen and connect, and I can put the connected port into non-blocking mode. So that's useful. I can read from it with $client->recv($buf, 512).

    So, in a non-blocking read, how do I detect EOF? Or errors? Normally recv() returns undef on error, but the doc on the socket method doesn't mention that. And calling recv() non-blocking when there's no data present returns an empty buffer, same as eof normally does, with no way to distinguish. So I tried calling $client->eof(), and that returns true all the time, which is just wrong; dunno what's up with that.

    Maybe using recv() and not just getline is over-complicating things, but eventually I expect the guts of my child process will be a tight loop that copies both directions between two sockets -- read A non-blocking, if data, write B, read B non-blocking, if data, write A, lather, rinse, repeat. Except with eof checks. So not having either side wait for end-of-line, or process the characters, seems cleaner and more efficient.

    Should I be using sysread instead of recv, maybe? I can't tell much from the docs. And the perl docs refer to the man pages for underlying system calls in this area a lot, but don't tell how things are mapped; in particular the system calls often have status returns that I can't see how to get from the perl functions. These details are getting very frustrating. The overall plan is simple, and many bits work, but what should be little simple stuff is holding me back a lot.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://926430]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-16 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found