转到:如何接收整个UDP数据报 [英] Go: How to receive a whole UDP Datagram
问题描述
我的问题:使用net.Read ...方法只复制给定字节数组或片的大小的字节数。我不想每次都分配64 kB的最大UDP数据报。
是否有一种方法可以确定数据报的大小(数据报头中的数据报的大小),还是再次读取,直到数据报被完全读取? nofollow> ReadFromUDP :
$ b
func(c * UDPConn)ReadFromUDP(b [] byte)(n int ,addr * UDPAddr,err错误)
ReadFromUDP从c读取UDP数据包,将有效载荷复制到b中。它返回复制到b中的字节数和数据包中的返回地址。
数据包的大小应该可以从 n
中获得,然后你可以使用它来定义一个自定义片段(或其他数据结构)来存储数据报。这依赖于数据报大小在会议期间不会改变,实际上不应该这样做。
My Problem: With net.Read... Methods copy only the number of bytes of the size of the given byte-array or slice. I dont want to allocate the maximum UDP datagram of 64 kB every time of course.
Is there a go way to determine the size of the datagram (which is in the datagram header) or read again until the datagram is completely read?
Try ReadFromUDP:
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error)
ReadFromUDP reads a UDP packet from c, copying the payload into b. It returns the number of bytes copied into b and the return address that was on the packet.
The packet size should be available from n
, which you can then use to define a custom slice (or other data structure) to store the datagrams in. This relies on the datagram size not changing during the session, which it really shouldn't.
这篇关于转到:如何接收整个UDP数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!