为什么从python套接字接收数据时会出现随机的OSError(22)无效参数异常?
我试图从python套接字接收一定长度的字节。有时它工作得很好,没有任何错误。但是随机的我得到了这个错误 OSError:[Errno 22]参数无效 以下是导致错误的代码:为什么从python套接字接收数据时会出现随机的OSError(22)无效参数异常?,python,sockets,networking,network-programming,Python,Sockets,Networking,Network Programming,我试图从python套接字接收一定长度的字节。有时它工作得很好,没有任何错误。但是随机的我得到了这个错误 OSError:[Errno 22]参数无效 以下是导致错误的代码: try: #Just a protocol header header = socket.recv(self.HEADERSIZE) #Length of the content, sent by the client length = int.fr
try:
#Just a protocol header
header = socket.recv(self.HEADERSIZE)
#Length of the content, sent by the client
length = int.from_bytes(header[1:len(header)], byteorder='big')
#line where the error is cause
byte = socket.recv(length)
return header[0], byte
except InterruptedError as e:
e.with_traceback()
标题:给出内容长度的5个字节
这条线很重要:
byte=socket.recv(长度)
有人知道我为什么会出现这个随机错误吗 你调试了长度。。。打印(长度)?是。看起来套接字没有读取完整长度的数据。例如,长度变量是8192,但套接字的读数是7949。然后在下一个数据包中,长度变量变为一个巨大的数字bc。在前一个数据包中,数据包没有被完全读取。如何修复套接字总是读取给定长度的字节?