I trying to understand how a HTTP server ensures the correct response is sent back to the correct client.
At a very high level:
At the TCP layer of the server implementation, some ServerSocket (listening on the host:port that the request was addressed to) creates a 'client socket' to handle the request
(if we assume its a threaded server) - a thread is allocated in the application and the work gets done
Questions:-
A.) Does the Response have to go back through the same Socket that handled the Request ?
B.) If Yes, how is the Response mapped to the same socket that handled the Request ?
C.) Is it the socket's responsibility to maintain the Client IP/host that the response packets need to get addressed back to, or is it the HTTP Headers which maintain this information and which is then used to address the response back to the correct client?
If the HTTP Header info is used to route the Response back to the calling client, then I assume the Response does not necessarily have to be handled by the same socket that handled the associated Request
Any help is much appreciated. James
Sockets are bi-directional.
When the ServerSocket receives a new connection, it creates a new Socket and hands it to the thread that will handle the request. This socket is already connected and supports two-way communication. This thread will then send the response back through this socket, which will cause it to be routed back to the connected client. The worker thread does not explicitly need to know the IP/host of the other end because the socket is bi-directional. It just needs to send its response through the socket and close the connection when it is done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With