c++ - 如何找出套接字在C++中有数据

标签 c++ linux sockets udp buffer

我编写了以下函数并且运行良好:

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/

相关文章:

sockets - rsocket-RDMA套接字API-客户端无法连接到服务器

c++ - 测量 C++ 代码的运行时间?

c++ - 使用 Visual Studio 编译器在编译时获取 vector 元素的大小(即使它是空的)

c++ - 什么是正确的 std::set_union 代码?

java - 使用 Java 获取每个内核的 CPU 负载

c++ - 从客户端应用程序调试动态库

java - 是否可以在没有连接设备的情况下写入串行端口?

c++ - Direct2D 接口(interface)和模糊文本问题

javascript - AngularJS 数据落后一步

c# - 异步服务器套接字缺少第一个缓冲区流