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

前端 未结 4 909
孤街浪徒
孤街浪徒 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: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.

提交回复
热议问题