基于select的socket编程

1.select函数

The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.

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).
int select(
  _In_    int                  nfds,
  _Inout_ fd_set               *readfds,//检测可读性的
  _Inout_ fd_set               *writefds,//检测可写性的
  _Inout_ fd_set               *exceptfds,//检测存在错误的
  _In_    const struct timeval *timeout   //select函数等待的最长时间,阻塞模式时设为0
);
typedef struct fd_set {
  u_int fd_count;  //集合中的数量
  SOCKET fd_array[FD_SETSIZE];
} fd_set;




....................
原文地址:https://www.cnblogs.com/freesec/p/6208425.html