You sent two messages and received one and a half
TCP and message framing
TCP is an ordered stream of bytes. Message boundaries are your protocol's job, usually a length prefix and a buffer.
Socket code that works locally can break under load, because TCP gives you an ordered, reliable stream of bytes, not messages. The boundaries between your writes are not preserved.
If a client sends HELLO then BYE, the receiver may see HELLO | BYE, or HEL | LOB | YE, or HELLOBYE in one read. The byte sequence is the same; read boundaries are not message boundaries, nor necessarily packet boundaries. So the fix is not "send each JSON in its own call".
recv(4096) does not mean "give me a whole message"
The number is a maximum. A read may return fewer bytes. On TCP, asking for a positive amount and getting zero means EOF after the peer closed its sending side cleanly, not "nothing right now". A non-blocking socket signals "nothing right now" with an error such as EAGAIN.
The fix: a protocol with framing
Send a 4-byte header before each payload: an unsigned big-endian integer holding the payload length in bytes. The receiver keeps a buffer and loops:
1. Accumulate until at least 4 bytes are present. 2. Read the length and check it against an agreed limit. 3. Wait until the whole payload has arrived. 4. Emit one message, then keep parsing the leftovers, which may hold more messages.
The header itself can arrive in pieces. The length counts encoded bytes, not characters, which matters for Hebrew and emoji.
Change the cut points and the output stays HELLO then BYE. A real server also needs a deadline for receiving a frame and limits on memory and connections. A length check alone is not a defence against a slow sender.
The trade-off
Length prefix suits binary data but needs a stateful parser and length limits. A delimiter such as a newline is simple for text, provided you define what happens when the delimiter appears inside content. If you use an existing protocol and a good library, the library already handles framing. Do not reinvent it.
Check yourself
The header says 10 bytes and one read returned the header plus 6 payload bytes. Is that a protocol error? No: keep the 6 and keep accumulating. EOF before the rest arrives leaves an incomplete frame, which must never be handed to the application as a valid message. Extra bytes from the next message are kept for the next parse round.