【问题标题】:How to make recv call non blocking and make it to wait only 5 second如何使 recv 调用非阻塞并使其仅等待 5 秒
【发布时间】:2018-11-22 11:35:09
【问题描述】:

如何使 recv 调用非阻塞并使其仅等待 5 秒!

// Receive until the peer closes the connection
do {

    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if ( iResult > 0 )
        printf("Bytes received: %d\n", iResult);
    else if ( iResult == 0 )
        printf("Connection closed\n");
    else
        printf("recv failed with error: %d\n", WSAGetLastError());

} while( iResult > 0 );

【问题讨论】:

    标签: timeout blocking winsock2 recv


    【解决方案1】:
    fd_set readfds;
    struct timeval count;
    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);
    select(0, &readfds, 0, 0, &count);
    if (FD_ISSET(sockfd, &readfds))
    {   
         if ((bytes=recv(sockfd, buffer2, 200, 0)) == -1) {
              return 0;
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 2012-01-03
      • 2010-12-22
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 2023-04-07
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多