As I know, tcp port in 'time_wait' stat cann't be used. However, in my experiment, server reuses the 'time_wait' port? Why?

Firstly, in client machine, type command ehco 40000 40001 > /proc/sys/net/ipv4/ip_local_port_range. So, the maximum number of TCP ports is 2.

server code

while (1) {
    int len = sizeof(struct sockaddr);
    fd = accept(sfd, &remote, &len);

    read(fd, buf, sizeof(buf));

client code

    for (i = 0; i < 3; i++)
        pid_t pid = fork();
        if (pid == 0)

 void handler()
      * ............. */

      res = connect(sfd, result->ai_addr, result->ai_addrlen);
      if (res == -1) {



[root@livecd ~]# ./client 
[root@livecd ~]# connect

It's up to 3 connections. I think, 2 connections at most. Why ? server has 2 timewait connections.

[root@livecd ~]# netstat -anp | grep TIME
tcp  TIME_WAIT                  
tcp  TIME_WAIT                              


Linux livecd.centos 2.6.32-642.el6.i686 #1 SMP Tue May 10 16:13:51 UTC 2016

server config

[root@livecd ~]# cat /proc/sys/net/ipv4/tcp_fin_timeout 
[root@livecd ~]# cat /proc/sys/net/ipv4/tcp_tw_recycle 
[root@livecd ~]# cat /proc/sys/net/ipv4/tcp_tw_reuse 

client config

[root@livecd ~]# cat /proc/sys/net/ipv4/ip_local_port_range 
40000   40001

Important I also try ubuntu server 14.04, but got the same result.

  • 2
    This might be better suited to Unix & Linux SE site as you seem to be using a live CD which doesn't sound like a production system.
    – zagrimsan
    Jul 27, 2016 at 7:25
  • I also try ubuntu server 14.04, but still got same result.
    – Pengcheng
    Jul 27, 2016 at 7:27

1 Answer 1


The Time Wait state is used prevent old packets from a previous connection from being accepted into a new connection. It effectively allows enough time for old packets to "die" in the network.

However, a socket in Timewait state can accept a new connection as long as the Initial Sequence Number on the SYN is higher than the last sequence number seen on the socket.

  • Sorry, I cann't understand your opinion. Sequence number is very large and increases with time. So in most cases, SYN's sequence number is larger than last sequence number. But, if last sequence number is up to the maximum, new SYN's sequence is smaller than last seq num. Last Fin seq number cann't be in new connction's sliding window, because seq num needs lots of time to up to maximum. I'm sure the time is much larger than 2MSL time. If you are right, why we need time_wait to "die" old packet?
    – Pengcheng
    Jul 28, 2016 at 1:52
  • TIME-WAIT is required to ensure that old duplicate packets from a previous connection are not accepted into a new connection where the duplicate packets sequence number happens to fall within the window of the new connection. Jul 28, 2016 at 7:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .