6

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.

Rella
  • 65,003
  • 109
  • 363
  • 636

2 Answers2

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.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
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