Active closure of server sockets

2019-08-28 22:22发布

I am having a small server (TCP server) which accepts at the max 10 connections on port 5000. I have created a socket in listen mode and accept connections. When accept succeeds, I create a new thread and handle traffic in that thread. I have a client on the same machine which is able to connect and communicate with this server.

Now to understand TIME_WAIT, I kill my server application using ctrl+c. I expect to see the server sockets which were in "Established" state to be transferred to "TIME_WAIT". However, when I do netstat after the closure, I dont see a single socket in that "TIME_WAIT" state. I know that sockets in "listen" mode directly transition to CLOSED state. But I am confused why sockets returned by accept and currently in Established state are not in TIME_WAIT state.

(I am on a linux machine and tcp_fin_timeout value is set to 1min.)

My tcpdump looks like below:

localhost.49388 > localhost.5000:
    Flags [S], cksum 0xfe30 (incorrect -> 0xaa93), seq 3264533269, win 32792,
    options [mss 16396,sackOK,TS val 20216234 ecr 0,nop,wscale 3], length 0              
localhost.5000 > localhost.49388:
    Flags [S.], cksum 0xfe30 (incorrect -> 0xc6a0), seq 3352338762, ack 3264533270, win 32768,
    options [mss 16396,sackOK,TS val 20216234 ecr 20216234,nop,wscale 3], length 0
localhost.49388 > localhost.5000:
    Flags [.], cksum 0xfe28 (incorrect -> 0x9fbe), ack 1, win 4099,
    options [nop,nop,TS val 20216234 ecr 20216234], length 0           
localhost.5000 > localhost.49388:
    Flags [P.], cksum 0xfe42 (incorrect -> 0xa300), seq 1:27, ack 1, win 4096,
    options [nop,nop,TS val 20216484 ecr 20216234], length 26           
localhost.49388 > localhost.5000:
    Flags [.], cksum 0xfe28 (incorrect -> 0x9db0), ack 27, win 4099,
    options [nop,nop,TS val 20216484 ecr 20216484], length 0          
localhost.49388 > localhost.5000:
    Flags [P.], cksum 0x0211 (incorrect -> 0x6be1), seq 1:1001, ack 27, win 4099,
    options [nop,nop,TS val 20216484 ecr 20216484], length 1000              
localhost.5000 > localhost.49388:
    Flags [.], cksum 0xfe28 (incorrect -> 0x91cb), ack 1001, win 6144,
    options [nop,nop,TS val 20216484 ecr 20216484], length 0     
localhost.5000 > localhost.49388:
    Flags [R.], cksum 0xfe28 (incorrect -> 0x8eeb), seq 27, ack 1001, win 6144,
    options [nop,nop,TS val 20217216 ecr 20216484], length 0

1条回答
ら.Afraid
2楼-- · 2019-08-28 23:29

*But I am confused why sockets returned by accept and currently in Established state are not in TIME_WAIT state.*

Because you killed the process, preventing any kind of orderly shutdown. You will see TIME_WAIT only if the process actually calls close() (or shutdown()) on a socket - which will send a FIN to the peer - and then remain alive long enough to receive both the ACK of the FIN sent to the peer and the peer's own FIN (for which an ACK will be sent back). There are three ways to reach TIME_WAIT, one of them must happen for TIME_WAIT to be established, but that can only happen if the process is still alive for the transition to FIN_WAIT1 which is the start of all three paths. Your Ctrl-C prevented even the close() that starts the sequence.

To see TIME_WAIT, you will have to arrange for your process to actively close a socket, e.g. in a handler installed for SIGHUP, or through some input mechanism. Killing the process at any point is a good way to ensure not seeing what you want.

查看更多
登录 后发表回答