Click here to Skip to main content
15,885,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I think they are using different ports.But when I find some information in the Internet,it show that they are using the same port.Can anyone tell why?Thank you.

For example:
My server machine listen port is 5555, when a client A to connect, the server creates a socket (port by means upstairs to 5555) to communicate, are there a port can be used in two places?
Subsequently, the client B to connect to the server and create a socket (port means in accordance with the upstairs is 5555) to communicate the same server with the emergence of three 5555 ports, Is this correct?
Posted
Updated 15-Dec-11 0:41am
v3

The server listens on the fixed port, and when a client connects it will redirect that connection to a dynamically created port. The client and server will now communicate through the sockets on the dynamic port, while the server continues to listen on the fixed port for other connection requests.
 
Share this answer
 
Richard - I think that's incorrect.

Each connection is identified uniquely, and distinct from all other connections on the Internet, using a 4-tuple comprising: IP of the server, port of the server, IP of the client, port of the client.

Since each different client connection will have a different IP and/or a different port, it's perfectly OK that the server uses the exact same numbers for IP and port for all of its clients. And that's exactly what happens.

The exact sequence (on the server side) is when a new connection is accepted (using a call to accept()), a new socket is created in the server. On the server side (i.e., the local side), the new socket has the same IP and port as all other sockets created by the server. On the remote side, however, the new socket has the IP and port of the client, thus differentiating the connection from all other connections to the server.

You might have heard of the term "ephemeral ports", which refers to the automatic selection of a new port number for each new outgoing connection. This concept is a client-side concept, not a server-side concept, and applies only to outgoing connections, not to incoming connections.

Mike
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900