win32 select學習

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

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兼容。app

readfds [in, out]socket

可選的指針 檢測socket的可讀性。async

writefds [in, out]ide

檢測可寫函數

exceptfds [in, out]ui

檢測錯誤this

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.

相關文章
相關標籤/搜索