• 0

Sending large message over TCP


Question

Hello, I have a little client-server application using tcp sockets. On both sides I use a buffer size of 4096 and I run all my tests with the client and server on the same machine, using address 127.0.0.1. Everything is going well except that now I suddenly need to send much larger messages. A message could now easily be 12MB in size. Now, what happens is that the transmission is incomplete : on the receiving end, I only get a chunk of the message instead of the whole thing.

I thought about simply increasing the buffer sizes on both ends to something larger than my maximum message size, but I'm not sure that's a reliable method, even if it works, and I'm not sure it'll work (if it's supported by TCP, if I won't run out of memory, etc.).

I also thought about splitting the message and actually my sending code automatically does that already, it will keep sending until all the data has been sent. However, on the receiving end, how do I know where a message ends, do I need to implement a protocol for that? I thought TCP already took care of that.

Anyway I'm a bit lost there, thanks for any tips.

Link to comment
https://www.neowin.net/forum/topic/809920-sending-large-message-over-tcp/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

The main way to doing this (which you may have realized) is to send header data with each packet sent. About 5 years back when I got into remote desktop applications (using VB6 and winsock) I'd send something like 0|DATA. Each time something was sent, an integer was prefixed with the character | as the splitter. Alternatively, if you aren't sure what characters may be sent through, you can instead use char(0) (invisible char if displayed but detectable w/ programming). I'd have constants/enumerator defining exactly what each pre-fixed integer means (something like const FIRST_DOWNLOAD_PACKET = 0; const NEXT_DOWNLOAD_PACKET = 1; const LAST_DOWNLOAD_PACKET = 2;) and so forth. Whenever the client application would receive a packet, I'd split the packet into 2 - header info and data. Header information told me what to do with the information while the data contained the actual information.

You could create a class to handle something like this too (something like a packet class) for re-using in other apps using sockets.

  • 0
  dlegend said:
The main way to doing this (which you may have realized) is to send header data with each packet sent. About 5 years back when I got into remote desktop applications (using VB6 and winsock) I'd send something like 0|DATA. Each time something was sent, an integer was prefixed with the character | as the splitter. Alternatively, if you aren't sure what characters may be sent through, you can instead use char(0) (invisible char if displayed but detectable w/ programming). I'd have constants/enumerator defining exactly what each pre-fixed integer means (something like const FIRST_DOWNLOAD_PACKET = 0; const NEXT_DOWNLOAD_PACKET = 1; const LAST_DOWNLOAD_PACKET = 2;) and so forth. Whenever the client application would receive a packet, I'd split the packet into 2 - header info and data. Header information told me what to do with the information while the data contained the actual information.

You could create a class to handle something like this too (something like a packet class) for re-using in other apps using sockets.

Yes, that's pretty much what I did, although more simple. My problem was basically how to send variable length messages; the receiving end has no idea where a message begins and where it ends. Sending fixed-sized messages is fine though, I can just buffer the input and split every [LENGTH] bytes. (where LENGTH is, of course, a constant)

So how I solved it is that I each time I send a message, I prefix it with a fixed-size header containing the length of the message. Another way I could have done it is to begin and end each message with a special character, but I was worried that could too easily break.

And yup this is all encapsulated in a nice, single-purpose, reusable class. :p

  • 0

the tcp protocol will ensure the data arrives in the correct order if you just break it up into chunks and send them off one after the other

the most stable/reliable method i've come across is where you send a packet and don't send anymore until the client responds with some form of acknowledgement

server sends 4KB

client responses "ok"

server sends 4KB

client responses "ok"

~ repeat

problem with that is the speed is now completely dependant on latency and you can't take advantage of burst techniques however if you're sending to a low latency network you shouldn't have many issues

always good to have various approaches for various network types to get the most out of them :D

  • 0
  DDStriker said:
the tcp protocol will ensure the data arrives in the correct order if you just break it up into chunks and send them off one after the other

the most stable/reliable method i've come across is where you send a packet and don't send anymore until the client responds with some form of acknowledgement

server sends 4KB

client responses "ok"

server sends 4KB

client responses "ok"

~ repeat

TCP handles acknowledgment and throttling for you automatically. If you're wanting to send data to a remote endpoint as fast as possible, you might as well send it in chunks of 4096 bytes and let TCP handle those. If throttling occurs, eventually send() will not return for awhile on the local side until the congestion has been alleviated.

  • 0

Since you're already splitting your messages at this point, you may want to reduce their size. 4096 byte packets are definitely going to be fragmented (unless this is a specialized closed network of some sort, this would even fragment on the old token ring), and that could result in latency due to retransmission. 1456 would be a typical size that would not fragment on most contemporary ethernet networks (1500 MTU, 20 byte IP header, 24 byte TCP header).

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • The Expanse: Osiris Reborn is a new narrative-driven sci-fi RPG inspired by Mass Effect by Pulasthi Ariyasinghe Out of nowhere, a narrative-driven sci-fi action RPG has been announced that will take players into the universe of The Expanse TV show and novels. The reveal trailer landed today during the Future Games Showcase, offering both a cinematic look at the setting as well as snippets of gameplay. Check out the The Expanse: Osiris Reborn debut trailer above. The studio behind the project is Owlcat Games. Some may remember that name from being involved in the role-playing titles Pathfinder: Kingmaker, Pathfinder: Wrath of the Righteous, as well as the most recent hugely well-received RPG Warhammer 40,000: Rogue Trader. Now, the studio's sights are set on The Expanse universe, and it's going for the over-the-shoulder third-person gameplay route for the first time. “We’ve been dreaming about building a sci-fi action RPG of this scale for a long time, and The Expanse is the perfect universe to bring that vision to life,” says Owlcat’s Creative Director, Alexander Mishulin. “It’s a world grounded in realism and complexity, perfect for telling a story the way we like it— mature and character-driven, where your choices truly matter." The story will have players taking the role of a custom captain that can be from the Earth, Mars, or the Belt to take control of the most advanced ship currently available. There's a crew to meet and lead, tactical third-person combat, and, as expected from this franchise, a divided solar system to navigate and make decisions on. The studio was also very direct about this experience being inspired by BioWare's Mass Effect trilogy. Aside from the action-heavy gameplay seen in the trailer, Owlcat is promising plenty of political intrigue, as well as romance options for players to dive into. Game Design Producer Yuliya Chernenko added "many of us first played it in our teenage years, and it left a lasting impression," and that "we are building on that legacy and expanding what players anticipate from this experience." The Expanse: Osiris Reborn does not have a release window just yet, but it will be coming out on PC (Steam, Epic Games Store, GOG), Xbox Series X|S, and PlayStation 5.
    • I don't know of anyway other than winareo tweaker to set the font type in Win11. They took that option away!   No wonder people are staying with Win10, or 7.   It's not our computer with Win11.... it's theirs!
    • It makes no sense to name it iOS 26 after iOS 18 but logic has left the building a long time ago (remember Microsoft and their Windows 8, 8.1 and 10 or Windows 7 that was actually 6.2)? Not to mention the stupid trend of "rapid releases" where minor releases with some bug fixes and UI changes (basically unnoticeable) masquerade as major releases...
    • Have you tried setting your font to Aptos? Its specifically designed to display better.
    • WebChangeMonitor 25.06 by Razvan Serea Monitors allows you to quickly check a number of web pages and tracks changes based on the content of the web pages. Allows to monitor several protocols, including HTTP and HTTPS. Allows to view and record differences. Available for Win7/10, Linux and others. WebChangeMonitor features: Allows monitoring of web pages and informs about content changes Indication of states of currently monitored items in the tool and taskbar Reporting as sound and/or email as well as log file or HTML log Several configuration / filter options Support all protocols, e.g. http, https Multi-threaded, running in the background Bulk-import and bulk-export of items (from/to CSV) to monitor Export of results to CSV file for further processing Allows running command on items states and/or showing diff (changes) of content with preferred diff-tool ...and many more! Open Source (C++, wxWidgets) Cross platform for Windows (7/10), Linux, RPi and Mac (if self-compiled) WebChangeMonitor 25.06 release notes: Updates several libraries to stay up-to-date with latest security fixes. Few bug-fixes took place related to ignore items being no displayed correctly sometimes in the UI. especially, the sqlite library was updated to hopefully also fix a recent (but very rare and random) crash bug. Download: WebChangeMonitor 64-bit | 8.9 MB (Open Source) Download: WebChangeMonitor 32-bit | 8.2 MB View: WebChangeMonitor Website | Other Operating Systems | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      488
    2. 2
      +FloatingFatMan
      256
    3. 3
      snowy owl
      246
    4. 4
      ATLien_0
      222
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!