So I wonder - is it possible to pass accepted TCP connection (on Windows or Unix like OS) from one process to another? Here the point is to pass connection - not data in a way a proxy app would.
Asked
Active
Viewed 2,899 times
2 Answers
8
In Unix, a TCP connection is represented as a socket file descriptor. When you fork
a process, the file descriptors are inherited by the child process, including TCP sockets. (Though they may be closed on exec
if given the FD_CLOEXEC
flag with fcntl
.)
It's also possible to transfer file descriptors between unrelated processes using a local (Unix) domain socket; see this question.
I'm not sure about Windows.
-
what does it mean? that two processes will share same connection? – Andrey Mar 15 '11 at 13:59
-
@Andrey: yes, so they must co-ordinate their access to the connection. – Fred Foo Mar 15 '11 at 14:02
-
1@Andry - A socket is just a file descriptor. Once you `accept()` a connection, you can do whatever you want with it, including sending it to another local process. – Brian Roach Mar 15 '11 at 14:03
-
A socket is not a file descriptor on Windows. – Lothar Nov 29 '15 at 05:12
5
On Windows, use WSADuplicateSocket
, pass the filled in WSAPROTOCOL_INFO
to the other process, use WSPSocket
to recreate a socket.
On unix-like OS'es this is possible using the sendmsg()
system call. libancillary abstracts this for you.

Erik
- 88,732
- 13
- 198
- 189
-
Is there any library that is alike libancillary for windows? and are out there any code samples / examples on how to do such thing on windows? – Rella Mar 15 '11 at 15:06
-
Don't know of any library - The only time I had to do this I just wrote it from scratch. See http://msdn.microsoft.com/en-us/library/ms740478%28v=vs.85%29.aspx – Erik Mar 15 '11 at 15:10
-
So.. I tried to create a sample using Boost.ASIO and Windows WSADuplicateSocket... Currently at least inside one process http://stackoverflow.com/questions/5326564/c-boost-asio-passing-accepted-tcp-connection-from-one-opened-socket-to-another may be you could posibly take a look and provide any help? Pleeease) – Rella Mar 16 '11 at 14:24