Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Im trying to send two packets over TCP, but it seems like they arrive “merged” or one packet disappears on the receiving end. Whats going on?

0
Posted

Im trying to send two packets over TCP, but it seems like they arrive “merged” or one packet disappears on the receiving end. Whats going on?

0

TCP is a stream protocol. Everything you push in one end will arrive, in order, out the other end. However, the “boundaries” you establish by calling send() on one end are not preserved in the stream. The TCP implementation may split the data such that multiple send() calls get merged into one recv() call, or so that a single, large send() gets received through multiple recv() calls. To construct message semantics on TCP, each time you send a message, you have to precede it with the length of the message, as a short or a long (use htons() or htonl() to make it cross-platform). Then write that many bytes of message. On the receiving side, you’d first read out the length (a short or long) and then that many bytes of message data, re-constituting each message sent from the other end.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123