[英]How asyncio UDP connection receives whole datagrams?
Python 庫中有一個接口asyncio.DatagramProtocol
。 它提供了通過使用方法datagram_received(self, data, addr)
來實現接收數據報的可能性:
class MyDatagramProtocol(asyncio.DatagramProtocol):
def datagram_received(self, data: bytes, addr: tuple[str, int]):
# Here I can use data as whole datagram.
# If there are more pending datagrams, it will called more than once
pass
異步事件循環接收整個數據報。 但是當我使用 BSD 套接字時,我應該使用全部或部分接收到的數據。 總數據可以大於一個數據報。 而且我不知道這些數據的范圍。 我只能使用自己的算法(讀取標題、正文中的字節數等)來解析它。
if (ioctlsocket(Socket, FIONREAD, (u_long*)(&PendingDataSize)) == 0)
{
BytesRead = recv(Socket, (char*)Data, PendingDataSize, Flags);
// Now I can use Data and parse it here.
// But Data can be two or more datagrams if remote machine sent two or more packets?
}
如何僅使用 BSD sockets 重復 asyncio 的行為?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.