以下从局域网聊天室抠出来的代码
问题是这里的nTimeOut是毫秒还是微妙,困惑? 听讲座是毫秒,
自己查了查是tv_usec 的赋值是微妙。(// and microseconds )
糊涂
- BOOL SOCKET_Select(SOCKET hSocket, int nTimeOut, BOOL bRead)
- {
- fd_set fdset;
- timeval tv;
- FD_ZERO(&fdset);
- FD_SET(hSocket, &fdset);
- nTimeOut = nTimeOut > 1000 ? 1000 : nTimeOut;
- tv.tv_sec = 0;
- tv.tv_usec = nTimeOut;
- int iRet = 0;
- if ( bRead ) {
- iRet = select(0, &fdset, NULL , NULL, &tv);
- }else{
- iRet = select(0, NULL , &fdset, NULL, &tv);
- }
- if(iRet <= 0) {
- return FALSE;
- } else if (FD_ISSET(hSocket, &fdset)){
- return TRUE;
- }
- return FALSE;
- }
复制代码 |