gpt4 book ai didi

c - 我将如何在 linux c 编程中使用 setsockopt 和 getsockopt 与 KEEP_ALIVE 来确定断开的 tcp/ip 连接?

转载 作者:可可西里 更新时间:2023-11-01 02:44:56 25 4
gpt4 key购买 nike

我将如何在 linux c 编程中使用 setsockopt 和 getsockopt 来确定断开的 tcp/ip 连接?

最佳答案

来自TCP man page :

To set or get a TCP socket option, call getsockopt(2) to read or setsockopt(2) to write the option with the option level argument set to IPPROTO_TCP.

以下是相关选项:

TCP_KEEPCNT(自 Linux 2.4 起)

The maximum number of keepalive probes TCP should send before dropping the connection. This option should not be used in code intended to be portable.

TCP_KEEPIDLE(自 Linux 2.4 起)

The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.

TCP_KEEPINTVL(自 Linux 2.4 起)

The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.

例子:

int keepcnt = 5;
int keepidle = 30;
int keepintvl = 120;

setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepidle, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(int));

关于c - 我将如何在 linux c 编程中使用 setsockopt 和 getsockopt 与 KEEP_ALIVE 来确定断开的 tcp/ip 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740492/

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