Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 569
Rella Avatar asked Mar 15 '11 13:03

Rella


People also ask

How client TCP process goes through different states?

The client sends a FIN packet to the server and updates its state to FIN-WAIT-1. The server receives the termination request from the client and responds with an ACK. After the reply the server will be in a CLOSE-WAIT state. As soon as the client receives the reply from the server, it will go to the FIN-WAIT-2 state.

Can you have two TCP connections on the same port?

What is the maximum number of concurrent TCP connections that a server can handle, in theory ? A single listening port can accept more than one connection simultaneously. There is a '64K' limit that is often cited, but that is per client per server port, and needs clarifying.

Can multiple processes use the same socket?

1 Answer. Save this answer. Show activity on this post. Two processes cannot bind (and listen) to the same unix socket.

How many processes can listen on a single TCP IP port?

For TCP, no. You can only have one application listening on the same port at one time.


2 Answers

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.

like image 58
Fred Foo Avatar answered Sep 28 '22 08:09

Fred Foo


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.

like image 37
Erik Avatar answered Sep 28 '22 07:09

Erik