gpt4 book ai didi

c++ - select 函数中等待 fd 的异常条件是什么?

转载 作者:行者123 更新时间:2023-11-30 01:17:02 29 4
gpt4 key购买 nike

选择函数的原型(prototype)是

select(int no_of_fd,fd_set read_fds,fd_set write_fds,fd_set expection_condition_fd,const struct timeval *timeout)

异常条件 fd 的 代表什么?

什么时候设置这些fd?

最佳答案

它用于:

  1. 未启用 SO_OOBINLINE 时出现带外数据。

  2. 检测 Windows 上的非阻塞 connect() 故障。如果连接成功,则将套接字放入write_fds,否则改为放入except_fds。这在 MSDN 上记录为 select()功能:

    In summary, a socket will be identified in a particular set when select returns if:

    readfds:

    • If listen has been called and a connection is pending, accept will succeed.
    • Data is available for reading (includes OOB data if SO_OOBINLINE is enabled).
    • Connection has been closed/reset/terminated.

    writefds:

    • If processing a connect call (nonblocking), connection has succeeded.
    • Data can be sent.

    exceptfds:

    • If processing a connect call (nonblocking), connection attempt failed.
    • OOB data is available for reading (only if SO_OOBINLINE is disabled).

    如果发生故障,您可以根据需要使用 getsockopt(SOL_SOCKET, SO_ERROR) 查询套接字以获取特定错误代码。

    在其他一些平台上,比如Linux,select()总是把socket放到write_fds中,不管连接成功还是失败,都要查询错误代码来区分。这记录在 connect(2) 的 Linux 手册页中功能:

    RETURN VALUE
    If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.
    ...
    EINPROGRESS
    The socket is nonblocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select(2) indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).

关于c++ - select 函数中等待 fd 的异常条件是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369586/

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