tags:

views:

417

answers:

1

In Linux, is it possible for me to open a socket and pass the socket to another process? If yes, can you please tell me where I can find an example?

Thank you.

+15  A: 

yes you can, using sendmsg() with SCM_RIGHTS from one process to another:

SCM_RIGHTS - Send or receive a set of open file descriptors from another process. The data portion contains an integer array of the file descriptors. The passed file descriptors behave as though they have been created with dup(2).

http://linux.die.net/man/7/unix

that is not the typical usage tho. more common is when a process inherits sockets from its parent (after a fork()). any file handles (including sockets) not closed will be available to the child process. so the child process inherits the parent's sockets.

a server process that listens for connections is called a daemon. this usually forks on each new connection, spawning a process to handle each new request. an example of the typical daemon is here:

http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC88

scroll down to void process().

jspcal