UDP数据是很特别的,因为它是由两部分组成的,一个UDP头+内容.UDP一个包最大长度过能超过64K,但一个64K的UDP包除非在内网中进行传输,不然在英特网中一上64K的UDP包能成功传送是基本可能性不大的.由于UDP包带有UDP头,报文头中带有目标地址,端口等信息的.如果不能一次性发完,例如只是发了一半,那么另一半就没了报文头了.这就是为什么UDP单个包要不就是全部发送成功,要不就是丢包,不会出现一个包被发成两部分发出去的.以下是msdn的原话:
"The sendto function is used to write outgoing data on a socket. For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying subnets, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned and no data is transmitted."
关键是"If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned and no data is transmitted".