我编写了以下函数并且运行良好:
bool NetworkSocket::isSocketReady()
{
/// Got here because iSelectReturn > 0 thus data available on at least one descriptor
// Is our socket in the return list of readable sockets
bool res;
fd_set sready;
struct timeval nowait;
FD_ZERO(&sready);
FD_SET((unsigned int)this->socketFD,&sready);
//bzero((char *)&nowait,sizeof(nowait));
memset((char *)&nowait,0,sizeof(nowait));
res = select(this->socketFD+1,&sready,NULL,NULL,&nowait);
if( FD_ISSET(this->socketFD,&sready) )
res = true;
else
res = false;
return res;
}
上面的函数当socket准备好工作时返回true,你知道我测试socket是否有数据我如何测试它吗?
最佳答案
when socket is ready for work return true
没有。当 read() 可以在没有 EWOULDBLOCK/EAGAIN 的情况下执行时返回 true,即如果有东西可以立即读取而不会阻塞。
Do you have any idea if i test socket has data
你已经找到了。
关于c++ - 如何找出套接字在C++中有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11755504/