Setting time out for connect() function tcp socket programming in C breaks recv()

前端 未结 4 850
孤街浪徒
孤街浪徒 2021-02-06 05:20

In my program If the server is not reachable the connect function take too much time. So i try to give time out to connect using select(). Now the problem is that when i try to

相关标签:
4条回答
  • 2021-02-06 05:39

    The socket should be set to blocking mode again before calling recv().

    fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) & ~O_NONBLOCK);
    
    0 讨论(0)
  • 2021-02-06 05:42

    The first successful select means the connect operation is complete but does not necessarily mean it succeed, from connect man page, you should check SO_ERROR to make sure it completed successfully

    It is possible to select(2) or poll(2) for completion by selecting the socket for writing. After select(2) indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).

    So in your code you should do something like this:

    int ret;
    ret=select(sockfd+1, NULL, &wfds, NULL, NULL); //should use timeout
    if(ret==1 && getSocketOpt(sockfd, SO_ERROR) ==0) {
        return 0; //successfully connected
    }
    

    Then, as mentioned in the other answer you should call select again before writing or reading from the socket.

    0 讨论(0)
  • 2021-02-06 05:44

    You receive EAGAIN because there's no data to read from socket buffer and your socket was set as nonblocking. Since you're not connected with the peer, i'm not surprised with it.

    Look at this from man recvfrom:

    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 set to EAGAIN. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

    Another case could be the following:

    • Your socket may be connected but you're too fast checking if something is received. To avoid this, put another select before recvfrom in order to extract the packet from the socket buffer (calling readfrom or just read) only when your're sure you received something.
    0 讨论(0)
  • 2021-02-06 05:50

    Setting time out for connect() function tcp socket programming in C is not working

    Correction. Setting the connect timeout is working. What 'isn't working' is the subsequent recvfrom(), and that's because you left the socket in non-blocking mode and you don't know what to do with the resulting EAGAIN. So, either handle that, by using select() to tell you when the socket is ready to read, or else put the socket back into blocking mode after finishing the connect.

    0 讨论(0)
提交回复
热议问题