使用 TCP 套接字发送多条小消息或更少的长消息更快 [英] Whats faster sending multiple small messages or less longer messages with TCP Sockets

查看:76
本文介绍了使用 TCP 套接字发送多条小消息或更少的长消息更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在任何地方找到任何文档,并且想知道尽快发送多条消息的最佳方法是什么.

I can find any documentatin anywhere and would like to know what would be the best method of send multiple messages as fast as possible.

例如,如果我有 300 台设备从一台服务器接收消息,最好发送一条大消息并让设备挑选出它们需要的部分或发送 300 条消息,但大小为 1/300.它们只会是小刺,因此 300 个设备每个只能获得 6 个字节

for example if I had 300 devices recieivng messages from one server would it be better to send out one big message and have the devices pick out the parts that they need or send 300 messages but at 1/300 of the size. They would only be small stings so the 300 devies would only be getting 6 bytes each

有区别吗?

先谢谢了.

推荐答案

如果启用了套接字的 Nagle 算法,那么通过给定连接发送多个较小的消息通常比通过同一连接发送较少的较大消息要慢.Nagle 在内部缓冲出站数据,并且必须等待足够的数据被缓冲和/或超时,以便它可以发送有效的消息.对于一般的套接字使用,启用 Nagle 通常是首选,因为它可以在大多数应用程序可接受的速度、性能和开销之间提供良好的平衡.但如果您需要发送对时间敏感的消息,那么您通常必须禁用 Nagle,以便尽快单独传输每条消息.

If the socket's Nagle algorithm is enabled, then sending multiple smaller messages over a given connection will generally be slower than sending fewer larger messages over that same connection. Nagle buffers outbound data internally and has to wait for enough data to be buffered and/or timed out so it can send efficient messages. For general socket usage, having Nagle enabled is usually preferred as it offers a good balance between speed, performance, and overhead that is acceptable to most apps. But if you need to send time-sensitive messages then you usually have to disable Nagle so every message is transmitted individually as soon as possible.

不过,TCP 无法实现您所描述的向多个设备发送一条消息.据推测,每个设备都有自己的直接与服务器的 TCP 连接.要将单个消息发送到所有 300 个连接,您必须制作 300 个独立的消息副本,每个连接一个.TCP 没有广播功能(如果需要,请切换到 UDP 或多播).发送单个服务器消息并让 300 个设备响应它的唯一方法是,如果设备不直接连接到服务器,而是通过代理进行通信,该代理保持与服务器的单一连接并将接收到的服务器消息转发给每个需要的设备.

What you describe about sending a single message to multiple devices is not possible with TCP, though. Presumably the devices each have their own TCP connection directly with the server. To send a single message to all 300 connections, you would have to make 300 independant copies of the message, one to each connection. TCP has no broadcasting capabilities (switch to UDP or Multicasting if you need that). The only way to send a single server message and have 300 devices respond to it is if the devices are not connecting to the server directly, but instead are communicating through a proxy that maintains a single connection to the server and forwards received server messages to each device as needed.

这篇关于使用 TCP 套接字发送多条小消息或更少的长消息更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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