本文实例讲述了C++中I/O模型的select模型用法。分享给大家供大家参考。具体实现方法如下:
void main()
{
CInitSock initSock;
USHORT nPort = 9999; //监听的端口
SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sListen == INVALID_SOCKET)
{
printf("socket error...");
return;
}
sockaddr_in servAddr = {0};
servAddr.sin_family = AF_INET;
servAddr.sin_port = ::htons(nPort);
servAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (SOCKET_ERROR == ::bind(sListen, (sockaddr*)&servAddr, sizeof(servAddr)))
{
int nError = ::GetLastError();
printf("connect error..");
return;
}
::listen(sListen, 5);
//select模型处理过程
//1.初始化套接字集合,添加监听socket到这个集合
fd_set fdSocket;
FD_ZERO(&fdSocket);
FD_SET(sListen, &fdSocket);
while (TRUE)
{
//2. 将集合的一个拷贝传递给select函数
//当有事件发生时,select移除未决的socket,然后返回 就是说select返回时 集合的中socket就是发生事件的socket
fd_set fdRead = fdSocket;
int nRet = ::select(0, &fdRead, NULL, NULL, NULL);
if (nRet > 0)
{
//通过比较原来的fdSocket集合与经过处理的fdRead集合
for (UINT i=0;i<fdSocket.fd_count;i++)
{
if (FD_ISSET(fdSocket.fd_array[i], &fdRead)) //就是这个触发了
{
if (fdSocket.fd_array[i] == sListen) //监听套接字接收到新连接 为神马分两种情况:因为触发Read集合的情况有两种:有连接来了或数据可读了....
{
//
if (fdSocket.fd_count < FD_SETSIZE) //FD_SETSIZE=64
{
sockaddr_in addrRemote = {0};
int nAddrLen = sizeof(addrRemote);
SOCKET sNew = ::accept(sListen, (sockaddr*)&addrRemote, &nAddrLen);
FD_SET(sNew, &fdSocket);
}
else
{
printf("too many connection...error");
continue;
}
}
else //有可读的
{
char szContent[256]={0};
int nRecv = ::recv(fdSocket.fd_array[i], szContent, sizeof(szContent), 0);
if (nRecv > 0)
{
szContent[nRecv] = '\0';
printf("recv data:%s", szContent);
}
else //没读到数据
{
::closesocket(fdSocket.fd_array[i]);
FD_CLR(fdSocket.fd_array[i], &fdSocket);
}
}
}
}
}
else
{
printf("nRet litter 0, error...");
return;
}
}
::closesocket(sListen); //与socket配对写
printf("*******************************");
getchar();
}
效果如下图所示:
希望本文所述对大家的C++程序设计有所帮助。