线程不更新但阻塞recv()

时间:2021-04-02 23:58:30

I am making a multi-threaded TCP server, when I use recv() in the threads, they do not update/execute/run infinitely (looping), unless recv() actually receives some data.

我正在创建一个多线程TCP服务器,当我在线程中使用recv()时,它们不会无限更新/执行/运行(循环),除非recv()实际接收到一些数据。

Here is a code snippet from inside the loop.

这是循环内部的代码片段。

if( seconds < 15 ){
    printf("%f seconds passed: \r", seconds);

    if ( (read_size = recv(sock , client_message , 512 , 0)) > 0 )
    {
      //Send the message back to client
      reply_message = client_message;
      (void)write(sock, reply_message, strlen(reply_message));
    }

}else{
    // ... blah blah blah
}

If I comment out the internal IF statement, the thread runs & outputs printf() as fast as it can. When the IF statement is included, the thread waits on recv() and will not update ( printf() isn't printed ) until recv() has received data :/

如果我注释掉内部IF语句,则线程会尽可能快地运行并输出printf()。当包含IF语句时,线程等待recv()并且不会更新(printf()不会打印),直到recv()收到数据:/

Any ideas?

1 个解决方案

#1


From recv(2) - Linux man page (see here):

来自recv(2) - Linux手册页(见这里):

If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and the external variable errno is set to EAGAIN or EWOULDBLOCK. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

如果套接字上没有可用的消息,则接收调用等待消息到达,除非套接字是非阻塞的(请参阅fcntl(2)),在这种情况下返回值-1并将外部变量errno设置为EAGAIN或EWOULDBLOCK。接收呼叫通常会返回任何可用的数据,直到请求的数量,而不是等待收到所请求的全部金额。

So it's the way how recv works. If you want it to return immediately you should use non-blocking mode (see fcntl and select desriptions).

所以这就是recv的工作方式。如果您希望立即返回,则应使用非阻塞模式(请参阅fcntl并选择desriptions)。

// sock is the socket you want to make non-blocking
int status = fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);

// handle the error
if(status == -1) {
    ...
}

If you want to check if there anything to read in socket

如果你想检查插座中是否有任何东西要读

int count = 0;
ioctl(sock, FIONREAD, &count);

count is the number of bytes available in the socket

count是套接字中可用的字节数

#1


From recv(2) - Linux man page (see here):

来自recv(2) - Linux手册页(见这里):

If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and the external variable errno is set to EAGAIN or EWOULDBLOCK. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

如果套接字上没有可用的消息,则接收调用等待消息到达,除非套接字是非阻塞的(请参阅fcntl(2)),在这种情况下返回值-1并将外部变量errno设置为EAGAIN或EWOULDBLOCK。接收呼叫通常会返回任何可用的数据,直到请求的数量,而不是等待收到所请求的全部金额。

So it's the way how recv works. If you want it to return immediately you should use non-blocking mode (see fcntl and select desriptions).

所以这就是recv的工作方式。如果您希望立即返回,则应使用非阻塞模式(请参阅fcntl并选择desriptions)。

// sock is the socket you want to make non-blocking
int status = fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);

// handle the error
if(status == -1) {
    ...
}

If you want to check if there anything to read in socket

如果你想检查插座中是否有任何东西要读

int count = 0;
ioctl(sock, FIONREAD, &count);

count is the number of bytes available in the socket

count是套接字中可用的字节数

相关文章