且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查是否使用Python中的TCP套接字接收了所有数据

更新时间:2023-11-29 19:29:16

使用套接字API,您不必担心TCP(排序,重新传输等),操作系统已解决了所有这些问题。唯一的事情是 socket.recv()将最多返回 n个字节。这意味着不能保证每次阻塞读取都会收到n个字节。因此,您必须循环阅读,直到收到所需的字节数为止。

using the sockets API, you do not have to worry about TCP (the ordering, retransmissions and whatnot), it's all been taken care of by the OS. The only thing is that socket.recv() will return at most n bytes. This means that it is not guaranteed that you will receive n bytes at each blocking read. So, you must read in a loop until you have received the desired amount of bytes.