Click here to Skip to main content
12,694,691 members (32,775 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C++ C# C#4.0 Sockets
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 15-Dec-11 0:33am
Updated 15-Dec-11 1:41am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 16 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100