UDP sendto() and recvfrom() max buffer size
I understand that the default max buffer size I can use with these functions is 65507 (5535 - IPv4 header - UDP header). However, is there a way to change this? I need to be able send a larger buffer ~66000 bytes. I tried us开发者_如何学编程ing the setsockopt() function, but it didn't seem to work.
Thanks!
No.
UDP only provides a datagram as the data part of an IP packet, an IP packet has a 16 bit length field thus limiting the data to 2^16 bytes including the headers, or 65507 bytes for the UDP data part(assuming no ipv4 options), there's no way to handle larger packets with UDP besides splitting them up in several packets and handle the reassembly etc. yourself.
Also it is quite likely to loose "big" UDP packets along the way, because the wrapping IP packet might be fragmented due to MTU limitations. Each of the fragments might get lost and there is no recovery mechanism in UDP. So while the theoretical limit for UDP payload is approx. 64kB the practical limit is around 1kB.
The UDP specification gives you 16bits in the UDP header for the packet size, that means you cannot send more than 65k at once. You cannot change this.
You have to split up your data into multiple packets. Using TCP instead of UDP will make the thing much simpler, since completeness and receiving order are guaranteed.
精彩评论