使用 TCP 时发送 1 个大块还是许多小块更好? [英] Is it better to send 1 large chunk or lots of small ones when using TCP?

查看:23
本文介绍了使用 TCP 时发送 1 个大块还是许多小块更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我accept()一个连接,然后write()到客户端socket之后,是一次性写入你打算发送的所有数据还是分块发送?

After I accept() a connection, and then write() to the client socket, is it better to write all the data you intend to send at once or send it in chunks?

例如:

接受,写入 1MB,断开连接

accept, write 1MB, disconnect

…或…

接受,写入 256 字节,写入 256 字节,... n,断开连接

accept, write 256 bytes, write 256 bytes, … n, disconnect

我的直觉告诉我,底层协议会自动执行此操作,并进行纠错等.这是正确的,还是应该对数据进行分块?

My gut feeling tells me that the underlying protocol does this automatically, with error correction, etc. Is this correct, or should I be chunking my data?

在你问之前,不,我不确定我从哪里得到了将数据分块的想法——我认为这是我从编程 C# Web 服务中获得的一种本能(为了绕过接收缓冲区限制等,我认为).坏习惯?

Before you ask, no I'm not sure where I got the idea to chunk the data – I think it's an instinct I've picked up from programming C# web services (to get around receive buffer limits, etc, I think). Bad habit?

注意:我使用的是 C

推荐答案

客户端和服务器会按照他们认为合适的方式拆分您的数据,这样您就可以在一个块中发送任意数量的数据.查看 Von Welch 撰写的 TCP Windows 用户指南文章.

The client and server will break up your data as they see fit, so you can send as much as you like in one chunk. Check A User's Guide to TCP Windows article by Von Welch.

这篇关于使用 TCP 时发送 1 个大块还是许多小块更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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