看来大家讨论那么多,感觉问题主要是:
1. 楼主问题描述有问题,TCP 包确实是错误概念,楼主想表达的应该是 走 TCP 协议的应用层包
2. 大家纠结在 TCP 包这个错误概念上,没有理解楼主的真实想法。
个人观点:
这个问题还是要处理的,TCP 并不知道应用层包的边界,recv 到的数据还是有可能包含一部分上一个(应用层)包尾和一部分下一个(应用层)包头。
附上一些国外网友的讨论:
"If you want to send and receive "packets" with TCP, you have to implement packet start markers, length markers and so on. How about using a message oriented protocol like UDP instead? UDP guarantees one send() call translates to one sent datagram and to one recv() call!"
ref:
https://gamedev.stackexchange.com/questions/96945/what-is-better-lots-of-small-tcp-packets-or-one-long-one"If you stick with SOCK_STREAM, then your receiving code needs to be prepared for the fact that a single call to recv may not retrieve a whole message as sent by the sender. recv will return the number of bytes actually received, or -1 if there is an error. If the messages are fixed length then you can repeatedly call it until you have the whole message."
ref:
https://stackoverflow.com/questions/24051965/maximum-limit-on-size-of-data-in-ipc-using-sockets-in-unix