我的本地网络上有以下套接字侦听:
def recvall(sock):
BUFF_SIZE = 4096 # 4 KiB
fragments = []
while True:
chunk = sock.recv(BUFF_SIZE)
fragments.append(chunk)
# if the following line is removed, data is omitted
time.sleep(0.005)
if len(chunk) < BUFF_SIZE:
break
data = b''.join(fragments)
return data
def main():
pcd = o3d.geometry.PointCloud()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('192.168.0.22', 2525))
print("starting listening...")
s.listen(1)
counter = 0
while True:
clientsocket, address = s.accept()
print(f"Connection from {address} has been established!")
received_data = recvall(clientsocket)
clientsocket.send(bytes(f"response nr {counter}!", "utf-8"))
counter += 1
print(len(received_data))
if __name__ == "__main__":
main()
172800我从手机上的应用程序向此端口发送字节长度的字节数据。正如您所看到的,我正在打印收到的数据量。time.sleep()仅当我按照上面的代码所示使用时,该金额才是正确的。如果我不使用这种方法,则只会收到一部分数据。
显然这是一些计时问题,问题是:我怎样才能确保在不使用的情况下一直接收到所有数据time.sleep()(因为这也不是100%确定有效,具体取决于设置的睡眠时间)
幕布斯6054654
相关分类