fd_set fdread;
FD_ZERO(&fdread);
FD_SET(pUDPServer->m_socketListen.m_socket, &fdread);
int maxSize = pUDPServer->m_socketListen.m_socket;
maxSize++;
包的结构体 包的结构体对象;
if (select(maxSize, &fdread, NULL, NULL, NULL) <= 0) continue;
//普通的UDP监视
if (FD_ISSET(pUDPServer->m_socketListen.m_socket, &fdread))
{
int iResult = pUDPServer->m_socketListen.RecvFrom((char *)&包的结构体, sizeof(包的结构体), &iIp, &sPort);
if ((iResult == 0)||(iResult == SOCKET_ERROR)) continue;
。。。。。。
}
m_socketListen是socket对象 可以是多个
m_socketListen 自己写的socket对象的 延伸
----------------------------
if(select(0, &fdRecv, NULL, NULL, &seltime) <= 0 || !FD_ISSET(pParam->sock, &fdRecv) )
continue;
Solidus 19:35:18
io复用
Solidus 19:35:45
拆开了写会死阿,晕死了
Solidus 19:36:26
fdRecv是一个fd_set
Solidus 19:36:53
里面存放着一组文件描述符(WIN32下可能就指当socket了)
Solidus 19:37:24
如果其中任何一个达到低潮标记,那么则函数返回
Solidus 19:37:37
FD_ISSET是用来测试是否是那个的
Solidus 19:38:13
&seltime是超时时间,这玩意现代人用的不算太多在win32下,效率太低
冷风 19:40:11
Solidus SOCKET 果然NB人啊
Solidus 19:40:47
也倒腾过一阵子
Solidus 19:41:07
I/O复用是linux下常用的东西,不过现在用的人也不多了,太慢
------------------------------------------------------------------------------------------------------
这句话你能理解吗?
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.
Solidus 20:33:10
select采用会阻塞这个线程
Solidus 20:33:49
然后内核轮旬被设置的描述符,如果被设置的有数据到达则返回
如果在FD_SET结构里有已准备和包含的socket handles总数
返回的fd_set包含有数据到达的socket
------------------------------------------------------------------------------------------------------
bool SocketImpl::Poll(t_u_long timeout, SelectMode mode) const
{
fd_set fdRead;
fd_set fdWrite;
fd_set fdExcept;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
FD_ZERO(&fdExcept);
if (mode & SELECT_READ)
{
FD_SET(m_sockfd, &fdRead);
}
if (mode & SELECT_WRITE)
{
FD_SET(m_sockfd, &fdWrite);
}
if (mode & SELECT_ERROR)
{
FD_SET(m_sockfd, &fdExcept);
}
int rc;
if(timeout == INFINITE)
{
rc = ::select(int(m_sockfd) + 1, &fdRead, &fdWrite, &fdExcept, 0);
}
else
{
struct timeval tv;
tv.tv_sec = (long)timeout;
tv.tv_usec = (long)0;
rc = ::select(int(m_sockfd) + 1, &fdRead, &fdWrite, &fdExcept, &tv);
}
if (rc == SOCKET_ERROR)
{
NetExptMsg msg = { "Poll Failure", ::WSAGetLastError() };
throw SocketException(msg);
}
return rc > 0;
}