gpt4 book ai didi

tcp - TCP close( ) 是否确保所有数据都传递给接收方

转载 作者:可可西里 更新时间:2023-11-01 02:43:27 26 4
gpt4 key购买 nike

我正在实现一个简单的代理应用程序,我总是从一端接收数据并发送到另一端。

在这种情况下,一旦我确定我已完成接收来自传入分支的所有数据,我是否可以直接调用 close( ) 而无需调用 shutdown( )?如果我这样做,close( ) 是否会确保所有数据都传送到传出线路上的目的地并由目的地的应用程序接收?

或者在这种情况下,是否必须在启动关闭之前启动关闭?

最佳答案

can I call directly close() without calling shutdown()

可以。 shutdown 仅在您想半关闭连接(即读取或写入结束)时才需要。

will the close( ) ensure that all the data is delivered to the destination on the outgoing leg and received by the application at the destination

close 在后台执行阻塞 send 执行的操作,不多也不少。如果后台 send 失败,您将不会收到任何错误指示。

还有 SO_LINGER 套接字选项,您可以根据需要进行调整:

  SO_LINGER
Sets or gets the SO_LINGER option. The argument is a linger
structure.

struct linger {
int l_onoff; /* linger active */
int l_linger; /* how many seconds to linger for */
};

When enabled, a close(2) or shutdown(2) will not return until
all queued messages for the socket have been successfully sent
or the linger timeout has been reached. Otherwise, the call
returns immediately and the closing is done in the background.
When the socket is closed as part of exit(2), it always lingers
in the background.

关于tcp - TCP close( ) 是否确保所有数据都传递给接收方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37685952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com