草庐IT

sockets - TCP_FASTOPEN 未声明

coder 2023-09-18 原文

我正在编写一个使用 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/

有关sockets - TCP_FASTOPEN 未声明的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  3. ruby - 如何使用 method_missing 动态声明方法? - 2

    我有一个ruby​​程序,我想接受用户创建的方法,并使用该名称创建一个新方法。我试过这个:defmethod_missing(meth,*args,&block)name=meth.to_sclass我收到以下错误:`define_method':interningemptystring(ArgumentError)in'method_missing'有什么想法吗?谢谢。编辑:我以不同的方式让它工作,但我仍然很好奇如何以这种方式做到这一点。这是我的代码:defmethod_missing(meth,*args,&block)Adder.class_evaldodefine_method

  4. 计算机网络笔记:TCP三次握手和四次挥手过程 - 2

    TCP是面向连接的协议,连接的建立和释放是每一次面向连接的通信中必不可少的过程。TCP连接的管理就是使连接的建立和释放都能正常地进行。三次握手TCP连接的建立—三次握手建立TCP连接①若主机A中运行了一个客户进程,当它需要主机B的服务时,就发起TCP连接请求,并在所发送的分段中用SYN=1表示连接请求,并产生一个随机发送序号x,如果连接成功,A将以x作为其发送序号的初始值:seq=x。主机B收到A的连接请求报文,就完成了第一次握手。客户端发送SYN=1表示连接请求客户端发送一个随机发送序号x,如果连接成功,A将以x作为其发送序号的初始值:seq=x②主机B如果同意建立连接,则向主机A发送确认报

  5. 已解决socket.timeout : The read operation timed out - 2

    已解决(pip安装模块超时,利用四种国内镜像源完美解决)WARENTING:Retrying(Retry(total=4,connect=None,read=None,redirect=None,status=None))afterconnectionbrokenby‘ConnectTimeoutError(pip._vendor.urllib3.connection.HTTPSConnectionobjectatOx00001D6OE4F4A940>,‘Connectiontopypi.orgtimedout.(connecttimeout=15)’)’':/simple/pip/socke

  6. ruby - 在 Ruby 中声明 "private"/"protected"时实际发生了什么? - 2

    在Ruby类定义中声明private/protected时实际发生了什么?他们不是keywords,这意味着它们必须是方法调用,但我找不到它们的定义位置。它们似乎没有记录在案。声明private/protected方法(如下所示)的两种不同实现方式是否不同?(第二种方式显然是方法调用,但在第一种方式中并不那么明显。)classFooprivatedefi_am_private;enddefso_am_i;endendclassFoodefi_am_private;enddefso_am_i;endprivate:i_am_private,:so_am_iend

  7. ruby - Cucumber 测试无法启动,错误为 "Display socket is taken but lock file is missing.." - 2

    运行cucumber后bundleexeccucumberfeatures/emails.feature:20我遇到了错误Displaysocketistakenbutlockfileismissing-checktheHeadlesstroubleshootingguide(Headless::Exception)/Users/me/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/headless-2.2.0/lib/headless.rb:195:inensure_xvfb_is_running'/Users/me/.rbenv/ver

  8. Ruby 模块声明 - 2

    这个问题在这里已经有了答案:Ruby(andRails)nestedmodulesyntax(4个答案)关闭2年前。做和做有什么区别吗classBus::Driverend和moduleBusclassDriverendend如果不是,首选哪种语法?

  9. ruby - 访问在另一个 rb 文件中声明的变量 - 2

    这是一个关于包含.rb文件的初级问题。我想访问在另一个rb文件中声明的数组。我的主程序是这样的:#!/usr/bin/envrubyload'price.rb'[...]max_price=price[az][type]*2[...]这是price.rb:price={'us-east-1'=>{'t1.micro'=>0.02,'m1.small'=>0.08,'c1.medium'=>0.165,'m1.large'=>0.320},'us-west-1'=>{'t1.micro'=>0.02,'m1.small'=>0.08,'c1.medium'=>0.165,'m1.larg

  10. ruby-on-rails - 如何在 Rails 中使用 acts_as_taggable_on 声明标签别名? - 2

    acts_as_taggable_on实现效果很好,但我还需要声明标签别名。我找到了一个声称这样做的插件,acts_as_taggable_with_aliases,但最后一次提交是在2009年并且不在gem存储库中,所以我认为该项目现在已经死了。有什么办法可以实现吗? 最佳答案 也许你可以创建自己的模型来支持这个(以及你想要的任何其他东西)......我认为您可以通过执行以下操作来实现:classTagtrueendclassModelIWantToBeTagged:taggableendmoduleModelTaggingdef

随机推荐