Hello, Marco: (Ewww, HTML mail..)
On Mon, Feb 06, 2012 at 12:36:35PM +0000, Marco van Kammen wrote: > I'm having difficulties with the following: > > Say you have a server program that listens on port 1234 The > server handles requests and spawns a child process for every > connection. > > If a client sends the text "foo" to the server, the server > itself should try to make a connection with another service on > port 4321. How can I accomplish that the server still responds > to incoming requests on port 1234 but also sends its commands > over the other socket to port 4321? > > I end up with my server talking over port 4321 but not > listening to commands on port 1234 anymore.. > > I've found some people talking about IO::Socket:Select but I'm > unable to find a simple example of how to implement something > above... All I want to accomplish is talking over 2 (or more) > sockets without the server hanging itself on one of the > sockets. > > Any help with examples or pointers in the right direction is > appreciated! I used IO::Select for bambot[1]. I use it to detect whether or not there is any data to read from STDIN (the "console") or an INET socket (the serveer) before attempting to do so to prevent the program from blocking. You basically just associate your stream handles with an IO::Select object and then periodically ask it if they're ready to read from, only doing so to the handles that are ready. There is an example in the POD for IO::Select that demonstrates a server that listens for connections and simultaneously handles multiple sockets. I guess if you can_read from a listening socket then there is a new connection ready to be accepted. You can view this example with either `perldoc IO::Select' or http://search.cpan.org/perldoc?IO::Select. I suggest you give it a shot and ask for help if you can't get the code working. If IO::Select is not enough then you could try non-blocking sockets. i.e., IO::Socket::INET->new( # ..., Blocking => 0, ); I'm unfamiliar with them so you'll need to read the docs to learn how to use them properly. Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bamccaig.com/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say' [1] https://github.com/bamccaig/bambot/
signature.asc
Description: Digital signature