Re: Passing a socket to a parent
by termix (Beadle) on Jun 04, 2002 at 01:57 UTC
|
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
| [reply] |
|
| [reply] |
|
Doh! Yeah.. I'm using Linux...
I thought you could pass a socket, similar to the way you can pass an open filehandle, but I certainly could be wrong...
- Ant
- Some of my
best work - (1 2 3)
| [reply] |
Re: Passing a socket to a parent
by samtregar (Abbot) on Jun 04, 2002 at 02:42 UTC
|
| [reply] |
|
| [reply] |
|
Does that mean you intend to solve the problem by forking thousands of children? If so, I don't think that will result in much of an improvement! Have you tried IO::Poll? Rumor has it that it's capable of better performance than good ol' select().
-sam
| [reply] |
|
Re: Passing a socket to a parent
by Abigail-II (Bishop) on Jun 04, 2002 at 12:01 UTC
|
Yes, you can. It's not portable though, so be aware.
It isn't trivial either, and there isn't any specific Perl
support for it. Under System V Release 4, you would use a
stream pipe and ioctl. On BSD, you would use a Unix Domain
Socket.
Details, including example code (in C), are to be found in
Stevens' Advanced Programming in the UNIX Environment
in the chapter Advanced Interprocess Communication.
Abigail | [reply] |
Re: Passing a socket to a parent
by perrin (Chancellor) on Jun 04, 2002 at 04:05 UTC
|
| [reply] |
|
Cool module, but since when are file descriptors called "access rights"? Shouldn't that Socket::PassFD?
-sam
| [reply] |
Re: Passing a socket to a parent
by perigeeV (Hermit) on Jun 04, 2002 at 16:58 UTC
|
This doesn't answer your direct question, but you may wish to know of the existance of Net::Gnutella. I haven't tried it myself though.
| [reply] |
|
| [reply] |