拆分 python 数据包?
python 有没有办法区分正在发送的数据包?例如
python 接收数据
它处理数据
客户端发送第一个数据包
客户端发送第二个数据包
python 接收数据,我可以接收第一个数据包而不是缓冲区中的所有信息
我知道我可以设置它,以便它发送我确认的数据,而客户端不会发送更多数据,我已确认已处理最后一部分,但我不想发送
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", 2000))
sock.listen(5)
所有相关的套接字数据
Is there a way python can distinguish between packets being sent ? e.g.
python receives data
it process data
clients sends first packet
client sends second packet
python receives data, can i receive the first packet rather then all info in the buffer
I know i can set it up up so it sends data i confirm and the client wont send more data it i have confirmed that have a processed the last piece but i'd rather not
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", 2000))
sock.listen(5)
all the relevant socket data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上有两种方法:
在每个数据包的开头,发送一个整数,指定该数据包的长度。当您接收数据时,首先读取整数,然后读取更多字节作为第一个数据包。
在数据包之间发送某种特殊标记。仅当您可以保证标记不会出现在数据包内时,这才有效。
正如 S. Lott 指出的,您可以使用 UDP(基于数据包)而不是 TCP(基于流),但这样您就放弃了 TCP 提供的其他功能(重传丢失的数据包、顺序数据包、和拥塞控制)。编写自己的重传代码并不难,但拥塞控制很难做到正确。
There are basically two approaches:
At the start of each packet, send an integer specifying how long that packet will be. When you receive data, read the integer first, then read that many more bytes as the first packet.
Send some sort special marker between packets. This only works if you can guarantee that the marker cannot occur within a packet.
As S. Lott points out, you could instead use UDP (which is packet-based) instead of TCP (which is stream-based), but then you give up the other features that TCP provides (retransmission of dropped packets, sequential packets, and congestion control). It's not too hard to write your own code for retransmission, but congestion control is difficult to get right.
是的。使用 UDP 代替 TCP。
Yes. Use UDP instead of TCP.