gpt4 book ai didi

sockets - TCP_FASTOPEN 未声明

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

我正在编写一个使用 TCP Fast Open 的小型服务器通过 setsockopt() 选项。但是我从 gcc 得到这个错误:

$gcc server.c
server.c: In function 'main':
server.c:35:34: error: 'TCP_FASTOPEN' undeclared (first use in this function)
if (setsockopt(sock, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen) == -1)

这是服务器的代码:

#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>

int main(int argc, char *argv[])
{
short port = 45000;
int max_conn = 10;
int fd = socket(AF_INET, SOCK_STREAM, 0);

if (fd == -1)
{
printf("Couldn't create socket: %s\n", strerror(errno));
return -1;
}

struct sockaddr_in ssi;
ssi.sin_family = AF_INET;
ssi.sin_port = htons(port);
ssi.sin_addr.s_addr = INADDR_ANY;

if (bind(fd, (struct sockaddr *)&ssi, sizeof(struct sockaddr_in)) != 0)
{
printf("Couldn't bind socket: %s\n", strerror(errno));
return -1;
}

// TFO
int qlen = 5;
if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1)
{
printf("Couldn't set TCP_FASTOPEN option: %s\n", strerror(errno));
return -1;
}

if (listen(fd, max_conn) != 0)
{
printf("Could'nt listen on socket: %s\n", strerror(errno));
return -1;
}

struct sockaddr_in csi;
int clen = sizeof(csi);

int cfd = accept(fd, (struct sockaddr *)&csi, &clen);

return 0;
}

为什么 gcc 报错?

TCP_FASTOPEN位于内核中的include/uapi/linux/tcp.h,它的值为23所以我试图在我的代码中重新定义它,然后它会编译并运行,但服务器不会发送该选项作为对 TFO 请求的答复(在 SYN-ACK 中)。

有人知道为什么吗?这与编译问题有关吗?

最佳答案

/proc/sys/net/ipv4/tcp_fastopen needs to be set to 2启用服务器端使用 TCP 快速打开选项:

The tcp_fastopen file can be used to view or set a value that enables the operation of different parts of the TFO functionality. Setting bit 0 (i.e., the value 1) in this value enables client TFO functionality, so that applications can request TFO cookies. Setting bit 1 (i.e., the value 2) enables server TFO functionality, so that server TCPs can generate TFO cookies in response to requests from clients. (Thus, the value 3 would enable both client and server TFO functionality on the host.)

此外,TCP_FASTOPEN 宏需要包含在 #include <netinet/tcp.h> 中.

关于sockets - TCP_FASTOPEN 未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553384/

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