This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

如何将recv设为非阻塞模式?



我们使用DaemonNew的例子进行tcp通信,用默认的阻塞模式recv工作正常。

但想把recv改为非阻塞模式,用了下面方法:

int sockblk = 0;

setsockopt(s, SOL_SOCKET, SO_BLOCKING, &sockblk, sizeof(sockblk));

但虽然recv变为非阻塞了,会马上返回,但返回值一直是-1,查询fdError后得到代码为EWOULDBLOCK;

无论client发送什么数据,recv的缓冲区里一直是0,recv返回值也一直是-1,无法接收数据。请问该如何将recv改为非阻塞模式工作?