54 points | by ncmv928 小时前
As you say, just lots of bad examples online. The only thing that made me a little bit crazy at the beginning, was the fact that TCP is a stream without delimiters, no packet concept. Other than that, is not more difficult than writing and reading a file.
[0] https://docs-archive.freebsd.org/44doc/psd/20.ipctut/paper.p...
Another useful resource: https://beej.us/guide/bgnet/
Edit: beej seems already cited and beloved
Since I didn’t see this in the list of references, I have to ask — have you heard of the (famous) Beej’s Guide to Network Programming [1]? It’s a classic (in the sense that it’s been around for a long, long time and goes into many more details) on this topic.
read <---- write
write ----> read
Implies that send/recv perfectly correlate to each other when they can be split up in stream sockets. Your code addresses that already with loops. So I know you know that. Just confusing diagrams I guess.