read() is not blocking in socket programming

前端 未结 3 1720
天命终不由人
天命终不由人 2021-01-02 12:51

I have a server that sends data to a client every 5 seconds. I want the client to block on read() until the server sends some data and then print it. I know read () is block

3条回答
  •  清歌不尽
    2021-01-02 13:15

    What is the value of MAXLINE?

    If the value is 0, then it will return 0 as well. Otherwise, as Grijesh Chauhan mention, set it explcitly to blocking.

    Or, you may also consider using recv() where blocking and non-blocking can be specified. It has the option, MSG_WAITALL, where it will block until all bytes arrived.

    n = recv(sockfd, recvline, MAXLINE, MSG_WAITALL);
    

提交回复
热议问题