ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I want to do followed thing.
Connect TCP socket in one process and work with it in another one.
It works fine if I fork second proc after connection establishing. But I need to to open sockets after fork().
I tried to use clone() with CLONE_FILES flag, it works. But clone() isn't POSIX api and it isn't supported by cygwin.
Yes, sharing a socket between two processes *IS* possible.
You need to use a "Unix domain socket" to pass the descriptor from the process that opened the socket to the process that you want to share it. You'll also need some mechanism for synchronizing the two processes.
I'd strongly recommend this book:
Unix Network Programming, W. Richard Stevens, ISBN 0131411551
You need to use a "Unix domain socket" to pass the descriptor from the process that opened the socket to the process that you want to share it.
Can you elaborate on that ?
Unix domain sockets are a way to pass *data* from a process to another.
How would you pass the socket descriptor to the receiving socket, and how this destination process can make use of this descriptor ?
No, Unix domain sockets also permit you to pass *socket file descriptors* between processes.
The basic trick is to:
1. open a socket of type "AF_UNIX" (instead of the more common "AF_INET") in each process
2. Create a pipe to join the two sockets
3. Do a sendmsg() on the sender, and a recvmsg () on the process you want to share the socket
I did a brief Google search, and didn't find any URL's I could point you to.
But it does work - trust me.
And it's discussed in detail in the Stevens book I mentioned above.
'Hope that helps .. PSM
An equivalent technique is available on Windows: you need to do an OpenProcess() and a DuplicateHandle ().
Sendmsg and Rcvmsg are indeed a means of sending data. However, they can also be used as a means of "exporting" a file descriptor (such as a socket) from one process and making it a valid file descriptor in a different, otherwise unrelated process.
Why don't you look it up (or better, try it) yourself, and share with the group what you learn? I've used this technique on other Unix's (and Windows) in years past, but I've never had the opportunity to try it on Linux. I'm definitely curious what you find.
I'm also interested in this, read through the example sources and still am trying to understand.
So if the fd is created in one process, say has value 3, and it's passed to another process does it still have the value 3 when it is received?
Is a new fd created somewhere, duplicated or otherwise modified, that I'm not seeing, that will be valid in the new process? Just trying to figure this out.