If my client connects to a server on port 80 on remote machine, the client socket will have my IP and a (random) port assigned to it. On the server side, the server listens on the port 80, when client connects, it accepts connection, gets a socket and these two sockets communicate. Now, my question is - What is the port number of the socket that is created by the server? (say by accept call of the server) Is the port number of this socket same (80) for all clients or does the server assign different ports for differtent client connections on server side? TIA, - Manish
if i understand you correctly, you have one computer that is a server (like a web server listening on port 80), and multiple clients connecting to it. in this scenario, each client would connect to port 80 on the server. the client itself would have a different (normally randomly assigned) port number. but the server will always be using port 80 - it does not open up another port for each client.
use the [CODE] tags - it makes it much easier for people to help you.
There seems to be a lack of clarity in your understanding of TCP\IP communication. The server starts up a given system by locking a specific port, in your case port 80, and proceeds to listen to sockets on the wanting to transmit on that port. The client comminicates/transmits to the server by connecting to it on that very port, i.e. the client sends a message to the server's IP on the port that the server is listening so that the server may hear it!
Originally posted by saager mhatre: There seems to be a lack of clarity in your understanding of TCP\IP communication. The server starts up a given system by locking a specific port, in your case port 80, and proceeds to listen to sockets on the wanting to transmit on that port. The client comminicates/transmits to the server by connecting to it on that very port, i.e. the client sends a message to the server's IP on the port that the server is listening so that the server may hear it!
I have got an answer to my question - The server DOES listen on the specified port (say 80) for incoming requests, once a connection is established it creates a different socket (in case of Java server the socket returned by ServerSocket.accept() call) for *each* client, which essentially binds to a different (arbitary) port so that server can continue to listen on its original port (say 80) for other client requests. The original server port (port 80) acts like a welcoming socket and there exists a connection like virtual pipe between the newly created socket on the server and the client socket. This is explained quite well in the Java Tutorial. So far so good. But I have a doubt - I made this small server which listens on port 2500. A client tries to connect to it on port 2500, the server accepts connection and creates a new Socket via accept call. When I call getLocalPort() on this newly created socket returned by accept(), it still returns 2500. Why??? Moreover, on my win 2K box, netstat -ap TCP shows sth like this -
why can't it show the connection between the client and the socket at diff port that is happening? Can smeone explain in details? Is there a way by which I can transperantly see this process? TIA, - Manish [ July 26, 2002: Message edited by: Manish Hatwalne ]
Joined: Apr 24, 2002
Manish - look at this page from the same tutorial: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html what is going on is that, as explained in other posts, there is only one server socket listening on the server. so when you type netstat, or get the localport from the serversocket instance, it will return the original port (2500 in your case). however, if you look at the ServerSocket API, there are no "read" or "write" methods. these calls must be done by a normal Socket. the ServerSocket class does have an accept() method that returns a normal socket. this is what you are looking for. try this: (i haven't tried it out, so please forgive any bugs - this probably needs to be in a try/catch block)
normally you don't need to worry what port the server has actually bound to the incoming connection. you simply use classes from java.io.* (such as BufferedReader and BufferedWriter) to access IO operations. was that helpful? read through the rest of the sun tutorial - i think it also explains multithreading the server and other advanced concepts. Jon
Hi Jon, Like I said in my earlier post, I did call getLocalPort() on the newly created socket, much similar to the code u have posted. And this new socket is returning me port 2500. There is no problem with multithreading and other advanced issues, for that matter, not even with working of the code. I am just trying to accurately visualize what is happening. Anyway, (this time) I did get the right picture. The server indeed creates a new socket per client, but ALL these new sockets bind on same port on which Server is listening (port 2500 in this case). This is possible because basic abstraction of TCP/IP is connction between two endpoints, and not portr (unlike UDP). The TCP segment that arrives at server will have address and port number of the source (client), so it will be sent to the socket which is connected to this client. This is explained very well in Doglous Comer's TCP/IP book. The curlprit here is probably the java tutorial section on Socket - Which explicitly says this - http://java.sun.com/docs/books/tutorial/networking/sockets/definition.html
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
I am not very sure, but I think this is incorrect information. Or maybe (more likely) I am missing some point in the tutorial. Thanks everybody for ur inputs, - Manish p.s. Can someone confirm whether the info in the tutorial is is incorrect or otherwise?