79379558

Date: 2025-01-23 00:46:46
Score: 0.5
Natty:
Report link

You can't assume (as you do seem to be assuming) that the data written by one side using a single send() call will be received by the other side in a single recv() call.

Depending on the amount of data and timing of network packet transmission, it's possible that send() will split your buffer into multiple packets, each of which will get received by a separate recv() call on the other side.

In other words, you need to implement some kind of command/response protocol, incorporating some kind of framing, on top of the byte streams that TCP is giving you.

You could alternatively switch from TCP to UDP, and use UDP packet boundaries for framing.

IMO the most natural algorithm would be to keep TCP, and use newline-terminated commands and responses. Ie, the command ends when you see a newline, not necessarily whenever recv() returns.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (0.5):
Posted by: AnotherSmellyGeek