套接字接收流收到FIN数据包后是否会关闭?

时间:2023-01-06 23:58:27

I am trying to figure out how to implement a graceful disconnection (4-way handshake).

我试图弄清楚如何实现优雅的断开连接(4次握手)。

First I send a FIN packet using the following code:

首先,我使用以下代码发送FIN数据包:

shutdown(socket, SD_SEND);

This will cause the sending socket send stream to be closed. Now when the other side receives the FIN packet, it will also send it's FIN packet:

这将导致发送套接字发送流关闭。现在当另一方收到FIN数据包时,它也会发送它的FIN数据包:

shutdown(socket, SD_SEND);

My question is: does receiving a FIN packet automatically closes a socket receive stream, and so there is no need to explicitly closing it:

我的问题是:接收FIN数据包是否会自动关闭套接字接收流,因此无需明确关闭它:

shutdown(socket, SD_RECEIVE);

1 个解决方案

#1


1  

Let's take your question in parts:

让我们分一些问题:

You state:

你说:

First I send a FIN packet using the following code:

首先,我使用以下代码发送FIN数据包:

shutdown(socket, SD_SEND)

关机(socket,SD_SEND)

This will cause the sending socket send stream to be closed. Now when the other side receives the FIN packet, it will also send it's FIN packet:

这将导致发送套接字发送流关闭。现在当另一方收到FIN数据包时,它也会发送它的FIN数据包:

First: The shutdown(socket} does not "close the send stream". It simply causes the socket to no longer accept send() calls. The send buffer is still there in the socket, you just can't put anything in it (because you said you wouldn't with the shutdown)

第一:关闭(socket}不“关闭发送流”。它只是导致套接字不再接受send()调用。发送缓冲区仍然在套接字中,你只是不能放入任何东西(因为你说你不会关机)

Second: You are correct that shutdown(socket, SD_SEND) should cause the socket to send a FIN. You are not correct that the other socket will send a FIN in response. The FIN your socket sends tells the other socket you will send no more data. The other socket may still have data it wants to send. The other socket will send a FIN when it also has no data to send. The other socket decides when to send a FIN.

第二:你是正确的,关机(套接字,SD_SEND)应该导致套接字发送FIN。你不正确,另一个套接字将发送一个FIN作为响应。您的套接字发送的FIN告诉另一个套接字您将不再发送数据。另一个套接字可能仍然有它想要发送的数据。当另一个套接字也没有要发送的数据时,它将发送FIN。另一个套接字决定何时发送FIN。

To answer your actual question:

要回答您的实际问题:

Receiving a FIN packet does not automatically "close a socket receive stream" as that is not something that can actually be done. The socket resources are only freed when closesocket(socket) is called. You can continue to call recv() even after a FIN has been received.

接收FIN数据包不会自动“关闭套接字接收流”,因为这不是实际可以完成的事情。仅在调用closesocket(套接字)时释放套接字资源。即使收到FIN,您也可以继续调用recv()。

#1


1  

Let's take your question in parts:

让我们分一些问题:

You state:

你说:

First I send a FIN packet using the following code:

首先,我使用以下代码发送FIN数据包:

shutdown(socket, SD_SEND)

关机(socket,SD_SEND)

This will cause the sending socket send stream to be closed. Now when the other side receives the FIN packet, it will also send it's FIN packet:

这将导致发送套接字发送流关闭。现在当另一方收到FIN数据包时,它也会发送它的FIN数据包:

First: The shutdown(socket} does not "close the send stream". It simply causes the socket to no longer accept send() calls. The send buffer is still there in the socket, you just can't put anything in it (because you said you wouldn't with the shutdown)

第一:关闭(socket}不“关闭发送流”。它只是导致套接字不再接受send()调用。发送缓冲区仍然在套接字中,你只是不能放入任何东西(因为你说你不会关机)

Second: You are correct that shutdown(socket, SD_SEND) should cause the socket to send a FIN. You are not correct that the other socket will send a FIN in response. The FIN your socket sends tells the other socket you will send no more data. The other socket may still have data it wants to send. The other socket will send a FIN when it also has no data to send. The other socket decides when to send a FIN.

第二:你是正确的,关机(套接字,SD_SEND)应该导致套接字发送FIN。你不正确,另一个套接字将发送一个FIN作为响应。您的套接字发送的FIN告诉另一个套接字您将不再发送数据。另一个套接字可能仍然有它想要发送的数据。当另一个套接字也没有要发送的数据时,它将发送FIN。另一个套接字决定何时发送FIN。

To answer your actual question:

要回答您的实际问题:

Receiving a FIN packet does not automatically "close a socket receive stream" as that is not something that can actually be done. The socket resources are only freed when closesocket(socket) is called. You can continue to call recv() even after a FIN has been received.

接收FIN数据包不会自动“关闭套接字接收流”,因为这不是实际可以完成的事情。仅在调用closesocket(套接字)时释放套接字资源。即使收到FIN,您也可以继续调用recv()。