In TCP Multi Threaded Server, if a client gets service ,how to find the port number of servicing socket?
From Sun Java tutorials
When a connection is requested and successfully established, the
accept()method returns a new “Socketobject” which is bound to the same local port and has its remote address and remote port set to that of the client. The server can communicate with the client over this newSocketand continue to listen for client connection requests on the originalServerSocket.
How can I find the port number of the “Socket object”?
Does
Socket.getPort()not do what you want? Or do you mean you want the local port (again, there’sSocket.getLocalPort()? If you could give a worked example of what you’re after, it would be easier to understand.Here’s a short example:
If you run that code and connect to it multiple times, you’ll get output something like this:
So
getLocalPort()returns the port that was specified in theServerSocketconstructor, butgetPort()returns a different port each time.