问题
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.
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
Does it make a difference?
Thanks in advanced.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/9651570/whats-faster-sending-multiple-small-messages-or-less-longer-messages-with-tcp-so