There's no guarantee that the characters send by the other side will all be sent in one message. So on the PuTty side, when the user types Reddit
return we don't know exactly how all this data will be sent over the net.
Your screenshot looks different from your narrative becasue ther's no seccond Reddit:
Invader said: RedditInvader said:
Here is what I understand based on it.
At the first loop:
- You'll receive 6 chars at once
- no error, so the first
if
body isn't executed
- no empty buffer, so the second
if
body isn't executed.
- You print
Invader said:
, followed by the chars received Reddit
- You send back to Putty a first message
The loop continues. At the second iteration:
- Your receive the equivalent of the return (1 or 2 chars that when printed out look like a newline)
- no error, so the first
if
body isn't exectuted
- no empty buffer, so the second
if
body isn't exectuted.
- You print
Invader said:
, followed by the chars received, which look like a newline
- You send back to Putty a second message
I can't tell more (breakpoint ? or recv()
still waiting because you didn't Ctrl+D ? ...)
That's the only explanation. If you would have received all the PuTty chars at once, you would get a different output:
Invader said: Reddit
Invader said: (or something else, but on a new line)
Remark 1: it's not linked to your problem, but if you encounter an error, your programme will keep trying to print output and looping, because you didn't foresee a break
in this case
Remark 2: it's not linked either, but if you receive exactly 4096 chars at once, your output might cause UB, because there would be a missing null terminator at the end of the buffer
Could be of interest for you: