Skip to content

Instantly share code, notes, and snippets.

@rahul619anand
Created May 20, 2017 09:39
Show Gist options
  • Select an option

  • Save rahul619anand/2596bbbeb1e82194d30bcb0096594e64 to your computer and use it in GitHub Desktop.

Select an option

Save rahul619anand/2596bbbeb1e82194d30bcb0096594e64 to your computer and use it in GitHub Desktop.
Dig deeper into TCP client server connections ...
Each TCP/IPv4 connection in general consists of two endpoints, and each endpoint consists of an IP address and a port number.
Therefore, when a client connects to a server, an established connection can be thought of as the 4-tuple of (server IP, server port, client IP, client port).
When a connection is established from client to server, the client side of the connection will be assigned a source port number (other than reserved range).
Unless a client program explicitly requests a specific port number, the port number assigned is an ephemeral port number.
Ephemeral ports are temporary ports assigned by a machine's IP stack, and are assigned from a designated range of ports for this purpose.
Note : As of Windows Server 2008, it uses a large range (49152-65535) by default.
Every TCP connection must terminate through the TIME_WAIT state before it is really completed.
The purpose of TIME-WAIT is to prevent delayed packets from one connection being accepted by a later connection.
Note: The TIME_WAIT state is twice the MSL (i.e. maximum segment lifetime) which, depending on the IP stack, is usually configured to be 120 seconds in Windows.
“Maximum Segment Lifetime is the time a TCP segment can exist in the internetwork system.
The side that actively closes the TCP connection (i.e. the side that sends the FIN command first) enters into the TIME_WAIT state for the set TCP TIME_WAIT interval.
So this port that was getting used is not allowed to be reused till the expiration of the TIME_WAIT interval.
When the connection terminates, the ephemeral port is available for reuse by other connections, although most IP stack implementation wouldn’t want to reuse that source port number until the entire pool of ephemeral ports have been exhausted.
So, if the client program tries to reconnect, it will be assigned a different ephemeral port number for its side of the new connection.
Considering almost 16000 distinct ports (which can be increased further by modifying the registry settings.) on the client side, it is highly unlikely that distinct clients when connecting to server will be reusing the ports.
When the max. connection is reached on the server , the server rejects connections. Hence a lower value of TIME_WAIT on the server would allow the port being used to be freed sooner, thereby decreasing the connection rejects.
1) To know the number of ephemeral ports on the system ,type the following command in the windows command prompt.
netsh int ipv4 show dynamicportrange tcp
2) To know the connections that have entered the TIME_WAIT state and also the ephemeral ports assigned to them, type the following command in the windows command prompt.
netstat –na | FIND “TIME”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment