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

北慕城南 提交于 2019-11-30 09:02:02

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5312773/c-application-is-it-possible-to-pass-accepted-tcp-connection-from-one-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!