[00020]-[2015-09-18]-[00]-[Windows Socket Select 模型]

int select(
int nfds;
fd_set FAR* readfds;
fd_set FAR* writefds;
fd_set FAR* exceptfds;
const struct timeval FAR* timeout;
);


#define FD_SETSIZE 64
typedef struct fd_set
{
u_int fd_count;
SOCKET fd_array[FD_SETSIZE];
}fd_set;

struct timeval
{
long tv_sec;
long tv_usec;
};

FD_CLR(); // 从set集合删除s套接字
FD_SET(); // 将s套接字添加到set集合
FD_ISSET(); // 判断s套接字是否在set集合
FD_ZERO(); // 将set集合初始化为空集合


ThreadFunc(void* pParam)
{
SOCKET sListen = .....;
FD_SET allfd;
FD_SET readfd;
FD_SET writefd;
FD_ZERO(&allfd);
FD_SET(sListen, allfd);

while(isRunning)
{
FD_ZERO(&readfd);
FD_ZERO(&writefd);
readfd = allfd;
writefd = allfd;
int nRet = select(0, &readfd, &writefd, NULL, NULL);

if(nRet > 0)
{
for(int i = 0; i < allfd.fd_count; i++)
{
if(FD_ISSET(allfd.fd_array[i], &readfd))
{
if(allfd.fd_array[i] == sListen)
{
// accept()
// addclient()
// FD_SET(sClient, &allfd);
}
else // 接收数据
{
pClient = GetClient(allfd.fd_array[i]);
pClient->RecvData();
}
}

if(FD_ISSET(allfd.fd_array[i], &writefd))
{
pClient = GetClient(allfd.fd_array[i]);
pClient->SendData()
}

}
}
}
}

posted @ 2015-09-18 13:56  Auris  阅读(142)  评论(0编辑  收藏  举报