Linux: is there a read or recv from socket with timeout?

前端 未结 5 1364
时光说笑
时光说笑 2020-11-27 10:42

How can I try to read data from socket with timeout? I know, select, pselect, poll, has a timeout field, but using of them disables \"tcp fast-path\" in tcp reno stack.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 11:15

    LINUX

    struct timeval tv;
    tv.tv_sec = 30;        // 30 Secs Timeout
    tv.tv_usec = 0;        // Not init'ing this can cause strange errors
    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv,sizeof(struct timeval));
    

    WINDOWS

    DWORD timeout = SOCKET_READ_TIMEOUT_SEC * 1000;
    setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
    

    NOTE: You have put this setting before bind() function call for proper run

提交回复
热议问题