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

I've set up a pretty generic bidirectional client (straight out of the Perl Cookbook, recipe 17.10) and forking server (using IO::All). Both sides use IO::Socket (IO::All is providing a front end to IO::Socket here.)

I'd like to able to terminate a client, and have a later instance of the client reconnect to a specific child process of the original server that the previous instance had been connected to.

My best thought so far is that I need to have the server child process open a socket on a new port, pass the port # back to the client, and close the old port; when the child gets the new port number, it closes the old socket, and opens a new one on the new port, storing the port # where later instances of itself can get to it.

Is there a smarter way to do this? Could it be done leaving the parent and children servers on one port and having the parent somehow magically forward its connection to a specific child based on info the child had previously passed back to the client?

Sorry for the vagueness of the questions -- I'm new to socket programming and trying to learn what's possible.

  • Comment on Reconnecting to a specific child of a forking server

Replies are listed 'Best First'.
Re: Reconnecting to a specific child of a forking server
by Fletch (Bishop) on Aug 16, 2004 at 13:14 UTC

    Depending on your OS it is possible to pass file descriptors between processes (although it does take deep POSIX black magic to do so). Find a copy of Stevens' Advanced Programming in the UNIX Environment and look in chapter 15 for Passing File Descriptors (p 479 in my copy). You could accept the connection in the master server process, look up which child if any handled that client previously in a hash, and then pass the socket off to whichever child (or one picked at random for a new client).

Re: Reconnecting to a specific child of a forking server
by ikegami (Patriarch) on Aug 16, 2004 at 16:06 UTC
    In the HTTP world, it was deemed better to save the state of the child rather than ensuring the same child was used again. This is done using sessions. This may not work for you, but you should at least have a look at it since a lot of (reusable) work was but into it.

      My inspiration in this is to save the overhead of saving/restoring state. (It's actually something a CGI script is going to be connecting to.)

      That would be the simpler tack, though, and perhaps worth doing.

Re: Reconnecting to a specific child of a forking server
by dws (Chancellor) on Aug 16, 2004 at 17:39 UTC