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

huangapple go评论52阅读模式
英文:

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

问题

当我的同事的Java(Client) TCP套接字程序向我的Python(Server) TCP套接字程序发送一个包(500KB~1MB)时,会出现分包和粘包的情况。我想知道,是否有一个通用的解决方案来解决这种情况?以及在这种情况下人们通常会做什么?
我认为如果可以手动刷新套接字函数recv(buffer_zone)请求的缓冲区,那么程序至少不会受到粘包的影响。

英文:

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. 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.

答案1

得分: 0

这是我,我已经通过头尾包模式解决了这个问题。
具体来说,当向服务器发送一系列数据时,客户端可以发送一个包含时间戳信息和数据长度的头部包。当服务器接收到来自客户端的头部包时,它可以知道客户端将要发送到服务器的数据长度,并且服务器可以为传入的数据准备相同长度的缓存。当客户端将尾部(数据)包发送到服务器时,服务器可以根据预先知道的数据长度取出数据。这是我目前的解决方案。

英文:

It's me, and I've solved this problem by Head-Tail Package Mode.
Specifically, when send a serial of data to the server, the client may send a head package including stamp information and data length. 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. 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. That's my solution for now.

huangapple
  • 本文由 发表于 2020年7月30日 16:29:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63169189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定