简体   繁体   English

可以为 python 的 socket.recvfrom() 设置最小字节数吗?

[英]Can a minimum number of bytes be set for python's socket.recvfrom()?

There are 2 types of packets Im sending from my server and trying to analyze:我从我的服务器发送了两种类型的数据包并尝试分析:

  • context packets of length 27 words (108 bytes)长度为 27 个字(108 个字节)的上下文数据包

  • data packets of length 250 words (1000 bytes)长度为 250 字(1000 字节)的数据包

I'm using python's socket recvfrom() method but documentation about this seems to be scarce.. Currently I have the following code snippet:我正在使用 python 的 socket recvfrom() 方法,但有关此的文档似乎很少。目前我有以下代码片段:

import socket


rx_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rx_socket.bind(("", 50000))

packets = []
for _ in range(10):
    data, _ = rx_socket.recvfrom(8192)
    packets.append(data)

My problem is that for each packet that ends up in packets list is a context packet.我的问题是,每个最终出现在packets列表中的数据包都是一个上下文数据包。 I know this because the length of each element is 27. I need to be able to receive the data packets, is there a way to specify a minimum number of bytes before recvfrom() returns?我知道这是因为每个元素的长度是 27。我需要能够接收数据包,有没有办法在 recvfrom() 返回之前指定最小字节数? Thank you for any help!感谢您的任何帮助!

is there a way to specify a minimum number of bytes before recvfrom() returns?有没有办法在 recvfrom() 返回之前指定最小字节数?

No. One can only specify the maximum amounts of bytes to read.不可以。只能指定要读取的最大字节数。

Note though that with datagrams (ie UDP) each recv will return exactly one datagram and send will send everything given in exactly one datagram, which also implies there is a 1:1 relationship between send on one side and recv on the other side.请注意,虽然对于数据报(即 UDP),每个recv将只返回一个数据报,而send将发送在一个数据报中给出的所有内容,这也意味着一侧的send和另一侧的recv之间存在 1:1 的关系。

Also it is not possible to pick datagrams based on there size and let the system ignore the others.此外,不可能根据那里的大小选择数据报并让系统忽略其他数据报。 Everything is delivered to the socket buffer (as long as there is room) in the order received.一切都按收到的顺序传送到套接字缓冲区(只要有空间)。 The only way to get to the larger datagrams is to explicitly remove every datagram received before by calling recv .获取更大数据报的唯一方法是通过调用recv显式删除之前收到的每个数据报。

My problem is that for each packet that ends up in packets list is a context packet.我的问题是,每个最终出现在packets列表中的数据包都是一个上下文数据包。 I know this because the length of each element is 27.我知道这是因为每个元素的长度是 27。

Well, you are reading only 10 packets and then stopping.好吧,您只读取 10 个数据包然后停止。 Is it possible that the data packets arrive after packet 10?数据包有可能在data包 10 之后到达吗?

I need to be able to receive the data packets我需要能够接收data

The recvfrom() call you already have will work perfectly fine for that purpose.您已经拥有的recvfrom()调用将非常适合此目的。 You are telling recvfrom() to read the next datagram, using a 8K buffer.您正在告诉recvfrom()使用 8K 缓冲区读取下一个数据报。 Your data packets are well within that size.你的data包在那个大小范围内。

If anything, the only thing you need to change is your loop condition, such as by using while True instead of for _ in range(10) .如果有的话,您唯一需要更改的是循环条件,例如使用while True而不是for _ in range(10)

If you are not receiving any data packets, that means either you are stopping your reading before they arrive, or they are simply not being sent to your listening port to begin with.如果您没有收到任何data包,这意味着您要么在它们到达之前停止读取,要么它们根本没有被发送到您的侦听端口。 Use a packet sniffer, like Wireshark, to verify that.使用数据包嗅探器(如 Wireshark)来验证这一点。

is there a way to specify a minimum number of bytes before recvfrom() returns?有没有办法在 recvfrom() 返回之前指定最小字节数?

No, there is not.不,那里没有。 Each call to recvfrom() will dequeue and return the next datagram on the socket, regardless of its size.recvfrom()的每次调用都将出列并返回套接字上的下一个数据报,无论其大小如何。 However, if the datagram is larger than the buffer size you specify, the datagram will be truncated.但是,如果数据报大于您指定的缓冲区大小,则数据报将被截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM