read() is not blocking in socket programming

前端 未结 3 1712
天命终不由人
天命终不由人 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:24

    What Greg Hewgill already wrote as a comment: An EOF (that is, an explicit stop of writing, be it via close() or via shutdown()) will be communicated to the receiving side by having recv() return 0. So if you get 0, you know that there won't be any data and you can terminate the reading loop.

    If you had non-blocking enabled and there are no data, you will get -1 and errno will be set to EAGAIN or EWOULDBLOCK.

提交回复
热议问题