#pragma once /************************************************************************//* seleect io模型的select操作封装 *//************************************************************************/ class Selector{public: Selector(void){reset();}; ~Selector(void){} inline void reset() { FD_ZERO(&fdSocket); } inline void add_sock(SOCKET s) { FD_SET(s, &fdSocket); }; inline int test_sock(SOCKET s) { return FD_ISSET( s , &fdSocket); } int select(int timeout=10) { struct timeval tv = {0, timeout}; //超时定义,10毫秒 int nRet = ::select(0, &fdSocket, NULL, NULL, &tv); //if (nRet==0) //超时 //if( nRet==SOCKET_ERROR ) // 出错 return nRet; } private: fd_set fdSocket; };
具体使用可以参考