On a (sufficiently) UNIX system when you create a child process by forking, the child process inherits open file descriptors from the parent (server parents most often use this to share the "listen" socket with a host of children). What you want to do is take a file descriptor created and obtained by the "child" to be handed back to the parent. I don't think that is possible (again, in UNIX).

You can certainly keep the child around and have it use a preexisting connection with the parent to keep using the connection it created. So everytime you create a child, have a file descriptor created between the parent and the child that is used by the child to "forward" traffic back to the parent. Then when the child starts, it can attempt a connection with the server and if succeeds, it tells the parent: which kills all the other children, and then deals with the child as the forwarder to the socket.

Another way to do this could be to have siblings that go at it, and the first successful connection has the parent kill all the other children. Then the successful child continues as the main application, while the parent hangs around to handle the signals etc. Coordination is still required, but the child does not have to be coded as a special gateway.

You can also use pipes and redirects to use STDIN/OUT for these purposes, but one thing I am pretty sure of is that the socket will be killed (shutdown) if your child that created it exits.

-- termix


In reply to Re: Passing a socket to a parent by termix
in thread Passing a socket to a parent by suaveant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.