服务器套接字的主动关闭 [英] Active closure of server sockets

查看:87
本文介绍了服务器套接字的主动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型服务器(TCP 服务器),它在端口 5000 上最多接受 10 个连接.我在侦听模式下创建了一个套接字并接受连接.当接受成功时,我创建一个新线程并处理该线程中的流量.我在同一台机器上有一个客户端,它能够连接到该服务器并与之通信.

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.

现在要了解 TIME_WAIT,我使用 ctrl+c 终止我的服务器应用程序.我希望看到处于已建立"状态的服务器套接字被转移到TIME_WAIT".但是,当我在关闭后执行 netstat 时,我没有看到处于TIME_WAIT"状态的单个套接字.我知道处于侦听"模式的套接字直接转换为 CLOSED 状态.但是我很困惑为什么接受返回且当前处于已建立状态的套接字不处于 TIME_WAIT 状态.

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.

(我在 linux 机器上并且 tcp_fin_timeout 值设置为 1 分钟.)

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

我的 tcpdump 如下所示:

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

推荐答案

*但我很困惑为什么由 accept 返回的套接字当前处于已建立状态而不处于 TIME_WAIT 状态.*

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

因为你杀死了进程,阻止了任何有序的关闭.只有当进程实际调用套接字上的 close()(或 shutdown())时,您才会看到 TIME_WAIT - 这将发送一个 FIN 到对等方 - 然后保持足够长的时间以接收发送给对等方的 FINACK 和对等方自己的 FIN(为此将发回 ACK).有三种方法可以到达 TIME_WAIT,其中一种方法必须在 TIME_WAIT 建立时发生,但只有当进程仍然活着以转换到 FIN_WAIT1 时才会发生是所有三个路径的开始.您的 Ctrl-C 甚至阻止了启动序列的 close().

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.

要查看TIME_WAIT,您必须安排您的进程主动关闭套接字,例如在为 SIGHUP 安装的处理程序中,或通过某种输入机制.随时终止进程是确保看不到您想要的内容的好方法.

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.

这篇关于服务器套接字的主动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆