我的代码如下
CUDP_CE *pSocket;
pSocket = (CUDP_CE*)lparam;
fd_set fdRead;
int ret;
TIMEVAL aTime;
char * recvBuf=NULL;
aTime.tv_sec = 1;
aTime.tv_usec = 0;
SOCKADDR_IN tmpAddr;
int tmpRecvLen;
int recvLen;
int iErrorCode;
char * recvedBuf = NULL;
int recvedBufLen;
while (TRUE)
{
//收到退出事件,结束线程
if (WaitForSingleObject(pSocket->m_ExitThreadEvent,0) == WAIT_OBJECT_0)
{
break;
}
//将set初始化空集合
FD_ZERO(&fdRead);
//将pSocket->m_UDPSocket套接字添加到集合中
FD_SET(pSocket->m_UDPSocket,&fdRead);
//调用select函数,判断套接字I/O状态
ret = select(0,&fdRead,NULL,NULL,&aTime);
if (ret == SOCKET_ERROR)
{
iErrorCode = WSAGetLastError();
pSocket->m_OnUdpError(pSocket->m_pOwnerWnd,iErrorCode);
break;
}
if (ret > 0)
{
if (FD_ISSET(pSocket->m_UDPSocket,&fdRead))
{
//要执行的代码
}
如果ret = select(0,&fdRead,NULL,NULL,&aTime);中变为ret = select(0,NULL,&fdRead,NULL,&aTime);ret就可以为1 不是零 还有一个现象就是fdRead在 select 执行之后为空 请各位大侠帮忙解决
你前一个是指读,,后一个是写,,,改天我也把select用在ce上玩玩,,
nfds specifies the maximum number of sockets to check.
EXAMPLE
In this example select waits for data to read from a socket.
#include
#include
#include
#include
/* This function calls select to wait for data to read from */
/* one of the sockets passed as a parameter. */
/* If more than 3 seconds elapses, it returns. */
/* Return value flags. These indicate the readiness of */
/* each socket for read. */
#define S1READY 0x01
#define S2READY 0X02
waittoread(int s1,int s2)
{
fd_set fds;
struct timeval timeout;
int rc, result;
/* Set time limit. */
timeout.tv_sec = 3;
timeout.tv_usec = 0;
/* Create a descriptor set containing our two sockets. */
FD_ZERO(&fds);
FD_SET(s1, &fds);
FD_SET(s2, &fds);
rc = select(sizeof(fds)*8, &fds, NULL, NULL, &timeout);
if (rc==-1) {
perror("select failed");
return -1;
}
result = 0;
if (rc > 0)
{
if (FD_ISSET(s1, &fds)) result |= S1READY;
if (FD_ISSET(s2, &fds)) result |= S2READY;
}
return result;
}