select函数的一点体会
select函数返回值
The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.
这是英文
自己的体会:如有新的连接,和 有数据发送到服务器, select后>=1,
附一段网上的代码,
这代码很多博客,文章都转载过,
我是菜鸟,觉得有些不对,高手莫笑,望指点
fdSocket是没select前的集合,代码没对fdSocket select,所所以其的套接字数目不变。
fdRead 则可能减少,也可能增多(比如:有新的连接)
所以for循环的执行次数,就不该用fdSocket ,而改用fdRead .
if(FD_ISSET(fdSocket.fd_array[i], &fdRead)) 这一句改成: if(FD_ISSET(fdRead.fd_array[i], &fdSocket))
if(fdSocket.fd_array[i] == sListen) // (1)监听套节字接收到新连接
这一句改成:
if(fdRead.fd_array[i] == sListen) // (1)监听套节字接收到新连接(fdRead是处理过剩下有连接或者有数据的套接字)
代码(转载的)
The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.
这是英文
自己的体会:如有新的连接,和 有数据发送到服务器, select后>=1,
附一段网上的代码,
这代码很多博客,文章都转载过,
我是菜鸟,觉得有些不对,高手莫笑,望指点
fdSocket是没select前的集合,代码没对fdSocket select,所所以其的套接字数目不变。
fdRead 则可能减少,也可能增多(比如:有新的连接)
所以for循环的执行次数,就不该用fdSocket ,而改用fdRead .
if(FD_ISSET(fdSocket.fd_array[i], &fdRead)) 这一句改成: if(FD_ISSET(fdRead.fd_array[i], &fdSocket))
if(fdSocket.fd_array[i] == sListen) // (1)监听套节字接收到新连接
这一句改成:
if(fdRead.fd_array[i] == sListen) // (1)监听套节字接收到新连接(fdRead是处理过剩下有连接或者有数据的套接字)
代码(转载的)
while(TRUE)
{
// 2)将fdSocket集合的一个拷贝fdRead传递给select函数,
// 当有事件发生时,select函数移除fdRead集合中没有未决I/O操作的套节字句柄,然后返回。
fd_set fdRead = fdSocket;
int nRet = ::select(0, &fdRead, NULL, NULL, NULL);
if(nRet > 0)
{
// 3)通过将原来fdSocket集合与select处理过的fdRead集合比较,
// 确定都有哪些套节字有未决I/O,并进一步处理这些I/O。
for(int i=0; i<(int)fdSocket.fd_count; i++)
{
if(FD_ISSET(fdSocket.fd_array[i], &fdRead))
{
if(fdSocket.fd_array[i] == sListen) // (1)监听套节字接收到新连接
{
if(fdSocket.fd_count < FD_SETSIZE)
{
sockaddr_in addrRemote;
int nAddrLen = sizeof(addrRemote);
SOCKET sNew = ::accept(sListen, (SOCKADDR*)&addrRemote, &nAddrLen);
FD_SET(sNew, &fdSocket);
printf("接收到连接(%s)\n", ::inet_ntoa(addrRemote.sin_addr));
}
else
{
printf(" Too much connections! \n");
continue;
}
}
else
{
char szText[256];
int nRecv = ::recv(fdSocket.fd_array[i], szText, strlen(szText), 0);
if(nRecv > 0) // (2)可读
{
szText[nRecv] = '\0';
printf("接收到数据:%s \n", szText);
}
else // (3)连接关闭、重启或者中断
{
::closesocket(fdSocket.fd_array[i]);
printf("关闭\n");
FD_CLR(fdSocket.fd_array[i], &fdSocket);
}
}
}
}
}
else
{
printf(" Failed select() \n");
break;
}
}
...全文
相关推荐
Select函数实现原理分析
Select函数实现原理分析Select函数实现原理分析Select函数实现原理分析Select函数实现原理分析
Pytorch中index_select() 函数的实现理解
主要介绍了Pytorch中index_select() 函数的实现理解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
linux c语言 select函数的用法
linux c语言 select函数的用法
select函数详细分析
select系统调用是用来让我们的程序监视多个文件句柄(file descriptor)的状态变化的。程序会停在select这里等待,直到被监视的文件句柄有某一个或多个发生了状态改变。
js 操作select相关方法函数
select的操作实现代码。方便控制select的朋友。脚本之家之前提供很多相关的代码。


