gpt4 book ai didi

linux - TCP/IP : set socket option for keep-alive after connection has been established

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

是否可以在一些假设的应用程序级握手后设置套接字的 SO_KEEPALIVE 选项(TCP 保持事件间隔和 TCP 保持事件值)?还是必须在调用 accept 之前设置?

我关心 Linux、Windows 和 eCos lwIP 堆栈之间的互操作性,因此非常感谢有关这两个平台的信息。

最佳答案

正如EJP所说,你可以随时设置。 man page说:

setsockopt() manipulate options for the socket referred to by the file descriptor sockfd.

你可以像这样设置或取消设置SO_KEEPALIVE

int iOption = 1; // Turn on keep-alive, 0 = disables, 1 = enables
if (setsockopt(socketHandle, SOL_SOCKET, SO_KEEPALIVE, (const char *) &iOption, sizeof(int)) == SOCKET_ERROR)
{
cerr << "Set keepalive: Keepalive option failed" << endl;
}

您还可以阅读此 tutorial了解更多详情。

关于linux - TCP/IP : set socket option for keep-alive after connection has been established,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31652295/

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