gpt4 book ai didi

c++ - "open"对于接受者来说意味着什么?

转载 作者:行者123 更新时间:2023-12-02 18:06:45 26 4
gpt4 key购买 nike

在boost.asio中,有一个类叫做acceptoracceptor 用于监听特定端点上的连接请求,并打开套接字以响应请求。

我感兴趣的有三个acceptor构造函数。

  1. 构造一个接受器而不打开或绑定(bind)
explicit basic_socket_acceptor(
const executor_type & ex);
  • 构造一个接受器并打开它
  • basic_socket_acceptor(
    const executor_type & ex,
    const protocol_type & protocol);
  • 构造一个接受器,打开它,并将其绑定(bind)到端点
  • basic_socket_acceptor(
    const executor_type & ex,
    const endpoint_type & endpoint,
    bool reuse_addr = true);

    1和3都有道理。 1 在 C++ 意义上构造接受器:在堆栈上分配内存,可能初始化一些变量等。 3 指定接受器应监听的端点。将接受发送至端点的传入连接请求,并在新套接字上创建连接。然而,2 似乎是一个中间立场,允许您设置“协议(protocol)类型”。

    我想说的是,“2 允许您指定比 1 更多的成员变量(协议(protocol)),并且比 3 指定更少的成员变量(例如,IP 地址和端口号)。”但是,我不知道它是否比设置一些成员变量做了任何“额外”的事情(它与操作系统或网卡有关吗?)。

    打开接受器意味着什么?

    最佳答案

    它确实打开了套接字。

    I chose to talk about the constructor to differentiate between 1, 2, and 3. A question about the member function open would probably have the same answer as this question.

    问题就变成了 ::socket 做什么 ( https://pubs.opengroup.org/onlinepubs/009604499/functions/socket.html ):

    The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets

    因此,为了您的实际理解,它分配并初始化内核资源[1]。

    注意

    从技术上讲,“接受器”套接字和“连接”套接字之间没有区别。 Asio 在概念上区分了它们,因此更容易正确使用它们。相反的问题可能会提供启示:Design rationale behind that separate acceptor class exists in ASIO

    [1] 在大多数操作系统上,用户态 TCP 堆栈都存在

    关于c++ - "open"对于接受者来说意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73076962/

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