简体   繁体   English

TCP连接中的FIN与RST

[英]FIN vs RST in TCP connections

The way I understand this, there are 2 ways to close TCP connection: 我理解这一点,有两种方法可以关闭TCP连接:

  • send FIN flag 发送FIN标志
  • send RST flag 发送RST标志

RST causes immediate connection termination, while in FIN you get a confirmation. RST导致立即连接终止,而在FIN中您会得到确认。

Do I understand this right, and are there any other distinctions between the two? 我是否理解这一点,两者之间是否存在其他区别? Can those 2 flags be used together? 这两个标志可以一起使用吗?

  • FIN says: "I finished talking to you, but I'll still listen to everything you have to say until you say that you're done." FIN说:“我告诉了你,但是在你说你已经完成之前,我仍会听你要说的一切。”

  • RST says: "There is no conversation. I won't say anything and I won't listen to anything you say." RST说:“没有谈话。我不会说什么,我也不会听你说的任何话。”

    RST is useful if you have long lasting TCP connection with little traffic. 如果您具有持久的TCP连接且流量很小,则RST非常有用。 If one of the computers is restarted, it forgets about the connection, and the other computer gets RST, as soon as it sends another packet. 如果其中一台计算机重新启动,它会忘记连接,另一台计算机一发送另一个数据包就会获得RST。

FIN or RST would be sent in the following case 在下列情况下将发送FIN或RST

  • your process close the socket 你的进程关闭套接字
  • OS is doing the resource cleanup when your process exit without closing socket. 当您的进程退出而没有关闭套接字时,操作系统正在执行资源清理。

    If your process call close(), FIN would be sent from the closing side by default (note: you can set socket option SO_LINGER to make it send RST instead of FIN) 如果您的进程调用close(),则默认情况下将从关闭端发送FIN(注意:您可以设置套接字选项SO_LINGER以使其发送RST而不是FIN)

    If your process exit without closing the socket, kernel would close the tcp connection and do the clean up for your process. 如果您的进程在没有关闭套接字的情况下退出,内核将关闭tcp连接并为您的进程进行清理。 FIN or RST can be sent. 可以发送FIN或RST。 If there is data in your receive queue, RST would be sent. 如果接收队列中有数据,则会发送RST。 Otherwise, FIN would be sent. 否则,FIN将被发送。

    You can loop through tcp_close() in tcp.c for more details.(I am using kernel-2.6.32-573.7.1 from redhat branch) 您可以在tcp.c中循环访问tcp_close()以获取更多详细信息。(我使用的是redhat分支中的kernel-2.6.32-573.7.1)

From RFC 1122, which everybody keeps citing, but not actually quoting, against me: 来自RFC 1122,每个人都引用, 但实际上没有引用,反对我:

A TCP connection may terminate in two ways: (1) the normal TCP close sequence using a FIN handshake, and (2) an "abort" in which one or more RST segments are sent and the connection state is immediately discarded. TCP连接可以以两种方式终止:(1)使用FIN握手的正常TCP关闭序列,以及(2)其中发送一个或多个RST段并且立即丢弃连接状态的“中止”。

It is not possible to use both at the same time. 不可能同时使用两者。 The concept doesn't even begin to make sense. 这个概念甚至没有意义。

It is possible by means of trickery which I will not describe here to close a TCP connection with an RST instead of a FIN, but it's a stupid idea, which is why I am not documenting it. 有可能通过技巧我不会在这里描述用RST而不是FIN关闭TCP连接,但这是一个愚蠢的想法,这就是我没有记录它的原因。 For one thing, all pending data in flight is lost. 首先,飞行中的所有未决数据都将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM