FIN vs RST in TCP connections

2019-01-21 22:34发布

The way I understand this, there are 2 ways to close TCP connection:

  • send FIN flag
  • send RST flag

RST causes immediate connection termination, while in FIN you get a confirmation.

Do I understand this right, and are there any other distinctions between the two? Can those 2 flags be used together?

3条回答
Emotional °昔
2楼-- · 2019-01-21 22:58

FIN or RST would be sent in the following case

  • 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)

    If your process exit without closing the socket, kernel would close the tcp connection and do the clean up for your process. FIN or RST can be sent. If there is data in your receive queue, RST would be sent. Otherwise, FIN would be sent.

    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)

查看更多
家丑人穷心不美
3楼-- · 2019-01-21 23:00

From RFC 1122, which everybody keeps citing, but not actually quoting, against me:

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.

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. For one thing, all pending data in flight is lost.

查看更多
Root(大扎)
4楼-- · 2019-01-21 23:10
  • 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."

  • RST says: "There is no conversation. I won't say anything and I won't listen to anything you say."

    RST is useful if you have long lasting TCP connection with little traffic. 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.

查看更多
登录 后发表回答