C++ application: Is it possible to pass accepted TCP connection from one process to another?
Asked Answered
N

2

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.

Nam answered 15/3, 2011 at 13:54 Comment(0)
M
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.

Morrie answered 15/3, 2011 at 14:9 Comment(3)
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?Nam
Don't know of any library - The only time I had to do this I just wrote it from scratch. See msdn.microsoft.com/en-us/library/ms740478%28v=vs.85%29.aspxMorrie
So.. I tried to create a sample using Boost.ASIO and Windows WSADuplicateSocket... Currently at least inside one process #5327064 may be you could posibly take a look and provide any help? Pleeease)Nam
T
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.

The answered 15/3, 2011 at 13:58 Comment(4)
what does it mean? that two processes will share same connection?Kermes
@Andrey: yes, so they must co-ordinate their access to the connection.The
@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.Scutch
A socket is not a file descriptor on Windows.Devine
M
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.

Morrie answered 15/3, 2011 at 14:9 Comment(3)
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?Nam
Don't know of any library - The only time I had to do this I just wrote it from scratch. See msdn.microsoft.com/en-us/library/ms740478%28v=vs.85%29.aspxMorrie
So.. I tried to create a sample using Boost.ASIO and Windows WSADuplicateSocket... Currently at least inside one process #5327064 may be you could posibly take a look and provide any help? Pleeease)Nam

© 2022 - 2024 — McMap. All rights reserved.