c++ - 使用 send() 发送大于 MSS 的数据

标签 c++ sockets tcp

通过TCP套接字发送数据的一般函数如下:

ssize_t send(int sockfd, const void *buffer, size_t length, int flags);

通常,如果数据大小大于 MSS,TCP 会将数据分段。但据说 send() API 将发送“长度”的数据量并返回可以发送到传出缓冲区的任何数据量,开发人员有责任检查返回值并重新发送剩余数据。

所以我怀疑,如果'length'值大于MSS,TCP不会自动对数据进行分片吗?如果不是,那么我们如何通过一次 send() 调用使 TCP 在内部对数据进行分段?

最佳答案

Generally, the TCP will fragment the data if data size is more that MSS.

一般不会。总是。否则 MSS 没有确定的意义。

But it is said that send() API will send the 'length' amount of data and returns whatever amount of data could be sent to outgoing buffer and its the responsibility of the developer to check the return value and resend the remaining data.

正确。

这两种说法并不冲突。 send() 将数据传送到套接字发送缓冲区; TCP 将其从那里传输到网络,并适当注意 MSS、拥塞、接收窗口等。

So my doubt it, then if the 'length' value is more than MSS, will not TCP automatically fragment the data?

见上文。 TCP 处理套接字发送缓冲区中的内容,而不处理提供给 send() 的参数值。

If no, then how can we make the TCP to fragment the data internally with one send() call?

你不能。您无法控制 TCP 分段。

关于c++ - 使用 send() 发送大于 MSS 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47986479/

相关文章:

c++ - 是否可以在 C++ 中增加变量范围?

javascript - 与远程服务器上的 Socket.io 建立连接

tcp - 使用 TCP 的 Stackoverflow?

java - setout 文件不会根据日期每天生成

java - 从tcp字节流中提取协议(protocol)数据单元的有效方法

c++ - CMake 包含和源路径与 Windows 目录路径不同

c++ - C++中的伪虚函数是什么?

c++ - 在 Linux 系统调用中的 C 程序中出现错误 ‘\342’ stray ‘\210’ stray ‘\222’

java - 如何在端口扫描器 java 实现中检测服务器状态

javascript - 尝试使用套接字通过 TCP 连接到远程计算机时出现“ECONNREFUSED”