|
Q & A on Demultiplexing TCP connections Q: One question occurred to me about a concurrent TCP/IP server is: If the server uses multithreading to achieve concurrency, what will happen if two threads read from their sockets, will there be any chance that they will mis-read a message of the others? i.e. at the time thread A recv(), the data in the TCP socket buffer is really for thread B, or vice versa. Even though two threads use different socket descriptor, all clients only send() to the same end point of IP address and port number of the server. I did not see that covered in book, please enlighten. A: You are correct in observing that all connections to a concurrent server use the same destination protocol port number. Of course, they all give the same destination IP address as well (the address of the computer on which the server runs). There is no ambiguity, however, because a TCP connection is identified by four items: the IP addresses of the computers at each end and the protocol port numbers used at each end. Thus, if data is arriving from two clients, TCP examines the client's IP address and protocol port number on each incoming segment to determine to which connection the data belongs. From a programmer's point of view, each socket corresponds to one connection; the only data received on the socket is from one client. |