When does socket.recv(recv_size) return?

后端 未结 3 436
粉色の甜心
粉色の甜心 2020-12-08 00:19

From test, I concluded that in following three cases the socket.recv(recv_size) will return.

  1. After the connection was closed. For example, the

3条回答
  •  轮回少年
    2020-12-08 00:50

    Yes, your conclusion is correct. socket.recv is a blocking call.

    socket.recv(1024) will read at most 1024 bytes, blocking if no data is waiting to be read. If you don't read all data, an other call to socket.recv won't block.

    socket.recv will also end with an empty string if the connection is closed or there is an error.

    If you want a non-blocking socket, you can use the select module (a bit more complicated than just using sockets) or you can use socket.setblocking.

    I had issues with socket.setblocking in the past, but feel free to try it if you want.

提交回复
热议问题