While reading up on how TCP works, I have read in quite a lot of books which mention that after listen()
, when a client connects to the server, accept()
should be called by the socket programmer to accept the connection from the client. The parent TCP server process (listening on port, lets say ,80) then calls a fork()
and a child process will be created which will then continue communicating with the client. The child process will obviously have to bind to another ephemeral port to communicate with the client. My question is how will the client know which port to send the data to in order to communicate once the child process is forked? Does the parent TCP process listening on port 80 convey it to the client?
Hiw does the client know the ephemeral port being used by the child TCP process?
There is no ephemeral port to know. The client just keeps using the same target port that it conncted to.
The child process will obviously have to bind to another ephemeral port to communicate with the client.
No. The client process inherits the accepted socket, which is bound to the same local port as the listening socket.
My question is how will the client know which port to send the data to in order to communicate once the child process is forked?
It communicates via the same port it connected to.
Does the parent TCP process listening on port 80 convey it to the client?
The client inherits the socket via the FD inheritance mechanism.