sendto 的最大缓冲区长度? [英] Maximum buffer length for sendto?

查看:28
本文介绍了sendto 的最大缓冲区长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于作为 UDP 端口打开的套接字,如何获得可以传递给 sendto(..) 调用的最大字节数?

解决方案

使用getsockopt().该站点 对您可以检索的用法和选项进行了很好的细分.

在 Windows 中,您可以:

<前>int optlen = sizeof(int);int optval;getockopt(socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (int *)&optval, &optlen);

对于 Linux,根据 UDP 手册页,内核将使用 MTU 发现(它会检查此处和目的地之间的最大 UDP 数据包大小,并选择那个),或者如果 MTU 发现关闭,则它'将最大大小设置为接口 MTU 并将任何更大的分段.如果您通过以太网发送,典型的 MTU 是 1500 字节.

How do you get the maximum number of bytes that can be passed to a sendto(..) call for a socket opened as a UDP port?

解决方案

Use getsockopt(). This site has a good breakdown of the usage and options you can retrieve.

In Windows, you can do:

int optlen = sizeof(int);
int optval;
getsockopt(socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (int *)&optval, &optlen);

For Linux, according to the UDP man page, the kernel will use MTU discovery (it will check what the maximum UDP packet size is between here and the destination, and pick that), or if MTU discovery is off, it'll set the maximum size to the interface MTU and fragment anything larger. If you're sending over Ethernet, the typical MTU is 1500 bytes.

这篇关于sendto 的最大缓冲区长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆