码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-09-18 15:09:52      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

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()
}

}
}
}
}

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

标签:

原文地址:http://www.cnblogs.com/Auris/p/4819022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!