简体   繁体   English

Python(服务器)和 Java(客户端)之间的 TCP 套接字中发生了意外的分包和粘连

[英]Unexpected Subcontract and Stick happened in TCP Socket between Python (Server) and Java (Client)

When my colleague's Java(Client) TCP socket program send a package (500KB~1MB) to my Python(Server) TCP socket program, there are cases of subcontracting and sticking.当我同事的 Java(Client) TCP socket 程序向我的 Python(Server) TCP 发送一个 package (500KB~1MB) 时,存在分包套接字程序的情况, I want to know, is there a universally valid solution to this situation?我想知道,这种情况是否有普遍有效的解决方案? And what people usually do in this case?在这种情况下人们通常会做什么? I think if it is possible to manually refresh the buffer zone requested by socket function recv(buffer_zone).If I can do that, the program will be free from sticking packages at least.我认为是否可以手动刷新套接字 function recv(buffer_zone) 请求的缓冲区。如果我能做到,那么程序至少不会粘包。

It's me, and I've solved this problem by Head-Tail Package Mode.是我,我已经通过 Head-Tail Package 模式解决了这个问题。
Specifically, when send a serial of data to the server, the client may send a head package including stamp information and data length.具体地,客户端在向服务器发送一串数据时,可能会发送一个头部package,包括戳信息和数据长度。 When the server receive the head package sent from client, it can know the length that client will send to server, and the server can preper the same length cache for incoming data.当服务端收到客户端发来的头部package时,就可以知道客户端要发给服务端的长度,服务端可以为传入的数据准备相同长度的缓存。 When the client send the tail (data) package to the server, the server can depend on the length of the data it knows in advance and take out the data.当客户端向服务端发送尾部(数据)package 时,服务端可以根据其预先知道的数据长度,将数据取出。 That's my solution for now.这就是我现在的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM