win32 select学习

来源:互联网 发布:网络00后女歌手名字 编辑:程序博客网 时间:2024/05/21 11:04

The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O.

select函数确定一个或者多个socket的状态,如果必要会等待,表现为同步IO。

int select(  _In_     int nfds,  _Inout_  fd_set *readfds,  _Inout_  fd_set *writefds,  _Inout_  fd_set *exceptfds,  _In_     const struct timeval *timeout);

参数:

nfds [in]

可以忽略。这个参数只对Berkeley socket兼容。

readfds [in, out]

可选的指针 检测socket的可读性。

writefds [in, out]

检测可写

exceptfds [in, out]

检测错误

timeout [in]

最大超时时间。

返回值

select函数返回已经准备好的socket handles的总数目,并且包含在fd_set数据结构中。如果超时返回0.如果有错误发生就返回错误。

注意

对于每个socket,调用者可以在读,写或者错误信息上请求。给定状态的socket组由fd_set结构给出。在fd_set结构包含的socket必须与一个单一的服务提供者相连。

参数readfds表示socket被检测可读。如果socket当前在监听状态,这将暗示一个新的链接来了,调用accept保证不会阻塞。对于其他可读的sockets,意味着可以调用recv,recvfrom来接受数据保证不会阻塞。

对于面向链接的socket,可读意味着对方可能已经关闭了socket。如果虚拟环路被优雅地关闭了,所有数据都收到了,然后recv得到0.如果虚拟环路被重置,recv将会受到错误。带外数据的到达也将被检测到如果socket的SO_OOBINLINE 选项开启。

参数writefds表示可读的sockets的结构。如果socket在处理非阻塞链接,socket是可写的。可以用send,sendto等发送,不会阻塞。然而,如果发送数据的大小超过了系统可用的buffer会阻塞。特别在多线程环境下,有效的长度是不能保证的。

 readfdswritefds, or exceptfds参数至少一个不能为NULL。

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

总结起来,当select返回,一个socket能够被认为:

readfds:

  • If listen has been called and a connection is pending, accept will succeed.
  • 如果listen已经调用,一个连接在等待,accpet将会成功
  • Data is available for reading (includes OOB data if SO_OOBINLINE is enabled).
  • 如果已经建立连接,说明data可读
  • Connection has been closed/reset/terminated.
  • 如果返回值为0,说明连接已经关闭/重置/终结。

writefds:

  • If processing a connect call (nonblocking), connection has succeeded.
  • 如果正在非阻塞connect,说明连接已经建立。
  • 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).
  • 带外数据可读了

Four macros are defined in the header file Winsock2.h for manipulating and checking the descriptor sets. The variable FD_SETSIZE determines the maximum number of descriptors in a set. (The default value of FD_SETSIZE is 64, which can be modified by defining FD_SETSIZE to another value before including Winsock2.h.) Internally, socket handles in anfd_set structure are not represented as bit flags as in Berkeley Unix. Their data representation is opaque. Use of these macros will maintain software portability between different socket environments. The macros to manipulate and checkfd_set contents are:

FD_CLR(s, *set)

Removes the descriptor s from set.

清除描述符

FD_ISSET(s, *set)

Nonzero if s is a member of the set. Otherwise, zero.

判断是否非0

FD_SET(s, *set)

Adds descriptor s to set.

增加描述符。

FD_ZERO(*set)

Initializes the set to the null set.

初始化描述符。

The parameter time-out controls how long the select can take to complete. If time-out is a null pointer, select will block indefinitely until at least one descriptor meets the specified criteria. Otherwise, time-out points to a TIMEVAL structure that specifies the maximum time that select should wait before returning. When select returns, the contents of theTIMEVAL structure are not altered. If TIMEVAL is initialized to {0, 0}, select will return immediately; this is used to poll the state of the selected sockets. If select returns immediately, then the select call is considered nonblocking and the standard assumptions for nonblocking calls apply. For example, the blocking hook will not be called, and Windows Sockets will not yield.

参数time-out控制select多长时间结束,如果设置为NULL,select将会阻塞一直到至少一个描述符满足要求。否则,timeout将会指出最长阻塞时间。如果timeout为0,select变为poll,就是非阻塞的。

Note  The select function has no effect on the persistence of socket events registered with WSAAsyncSelect orWSAEventSelect.

Note  When issuing a blocking Winsock call such as select with the timeout parameter set to NULL, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread. Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients.



原创粉丝点击