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

Two Perl programs are running on the same box. They are communicating with each other via a TCP connection via loopback. Both programs are also listening for incoming TCP connections on different ports. One of the programs receives an incoming TCP connection on one of the ports it is listening on. It accepts the connection and talks to the remote host for a while. It then wants to transfer the existing TCP connection to the other running Perl program, without dropping the connection and forcing the remote to reconnect, without the remote having to take any action at all, without the remote even knowing anything happened. In short, how does one Perl program hand off an IO::Socket::INET object to another program?
  • Comment on Passing a TCP connection from one Perl program to another

Replies are listed 'Best First'.
Re: Passing a TCP connection from one Perl program to another (APitUE++)
by tye (Sage) on Oct 16, 2009 at 04:09 UTC

    Instead of (or in addition to) talking over TCP sockets via localhost, they need to talk to each other "unix domain sockets". Then you can use Socket::PassAccessRights.

    - tye        

      and this works in Unix/Linux. In Windows, AFAIK, there is no equivalent functionality.
Re: Passing a TCP connection from one Perl program to another
by muba (Priest) on Oct 16, 2009 at 04:11 UTC

    I'm afraid the only way is to make the accepting program act as a proxy between the other program and the remote machine.

    So basically, it'd look like this:

    1. Program A: incoming connection
    2. Program A accepts and does whatever needs to be done
    3. Program A finds that it is now Program B's responsibilty. Either 1) using the existing TCP connection between A and B, or 2) by establishing a new connection, A informs B about this.
    4. Program A maintains the connection to the host but doesn't do anything except for bidirectionally relaying all data it receives from either party.
    5. When program B is done as well, it tells A about it, either by sending a specific code over the existing connection or simply closing the one created in step 3.2.
    6. Program A performs the goodbye rituals as per protocol (such as sending a 'everything succesful' sort of status message) and closes the connection to the remote as well.

    I could be mistaken, of course, but to my knowledge this is the only way to fulfill your requirements. It'd be much simpler, of course, if Program A could simply say "Hey, listen, I can't do anything for you anymore, but here, have this state information (session ID or just the session data, something like that, so that Program B will know what it's working with) and contact my friend, program B. You can find him on this-and-that address."