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?
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
- When I use NetSimple I send 9 packets from my NetSimple client to my NetSimple Server, but they get joined together and arrive as 2 packets. Is this normal?
- 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?
- How do I send a TCP/IP packet through a raw socket?