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
asyncio 事件回圈接收整個資料報。但是當我使用 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?
}
我怎樣才能重復 asyncio 的行為,但只使用 BSD 套接字?
uj5u.com熱心網友回復:
使用資料報套接字,recv一次總是只接收一個資料報。
請參閱手冊頁udp(7):
所有接收操作都只回傳一個資料包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462598.html
