I was going through an example on socket programming with multi threading. It has a socket server with multi threading implemented in it. I fail to understand the following: 1. A listener port is opened on the server say 8888 where the server listens to the incoming requests,from which port does it send the response back to the client? Is it from the same listener port or does it open any other free port to send the response? 2.The example does not open any port explicitly on the client machine,so from which port does the client receive and send the requests? 3.I understand that the server is capable of handling multiple requests because of multithreadig although i cannot understand that how the requests and corrosponding responses are mapped that is how is the manupilation done that client receive the response to their request and not somebody elses response and who does this manupilation? Basically i need to understand that whats going on behind the scenes for all these activities. Will appreciate a detailed answer. The example in question can be found at http://java.sun.com/docs/books/tutorial/networking/index.html. [ April 14, 2004: Message edited by: Michael Ernest ]
1) A TCP connection is specified by four numbers: the port on each end, and the IP address on each end. As long as one of these four is different, you've got a unique connection. Therefore, the server can use the same port to talk to many clients. 2) The client uses a "transient" port -- a randomly selected port in a range set aside for this purpose by the OS. 3) The operating system's TCP/IP stack handles it, in a layer below Java. A connection comes in from a given port at a given address, to a given port at a second address, and this set of four numbers is a unique identifier for the connection. The OS sorts out the details of receiving and delivering network packets. A "socket" is an abstraction of a pipe that connects two processes, through which data can be sent and received; the implementation of that abstraction is up to the OS. A socket is associated with one of those unique sets of four numbers.
Thanks for the reply. I have another query... 1. If my client is sending request from port xxxx then does it listens to the response from xxxx as well? My understanding is YES. If the client has to send n requests to the server then does it wait for the first response to come back and then send the second request or can it keep sending the request without getting the response? If it is the second case then will it have to have to wait for the second response before it gets the first one back else how does it maps the response to the corrosponding request?