如何处理在socket java上发送的大对象?

时间:2022-03-07 14:32:14

I am trying to implement a client-server in java
and i made connection between in sockets
and sending JSON objects as strings on streams
if i have big object is there's a way to handle it
so i don't have to regroup it because the limit size of tcp packet (cant know when the single object is fully transferred to me as client or not yet)
note :am using G-son to convert objects to JSON objects

我想实现一个java客户机-服务器,我在套接字之间的连接和发送JSON对象作为字符串流如果我有大对象有一种方法来处理它,所以我不需要重组,因为tcp包的限制大小(不知道单一对象时完全转移到我客户或没有)注:使用G-son将对象转换为JSON对象

2 个解决方案

#1


0  

is there's a way to handle it so i don't have to regroup it because the limit size of tcp packet

有没有一种方法可以处理它,这样我就不必因为tcp包的限制大小而重新分组了

You don't have to care about the size of TCP packets. Just write the data. TCP will segmentize and packetize it for you.

您不必关心TCP包的大小。写数据。TCP将为您分段并打包它。

(cant know when the single object is fully transferred to me as client or not yet)

(不知道什么时候将单个对象作为客户端完全转移给我)

Yes you can. You reach the closing '}', as @StephenC mentions. Your JSON parser should take of that for you in any case.

是的,你可以。正如@StephenC提到的,你到达了关闭的“}”。在任何情况下,JSON解析器都应该为您提供。

Your question is founded on false assumptions.

你的问题是基于错误的假设。

#2


1  

If I have big object is there's a way to handle it so I don't have to regroup it because the limit size of tcp packet. (I cant know when the single object is fully transferred to me as client or not yet)

如果我有一个大对象,有一个方法可以处理它,所以我不需要重新分组因为tcp包的限制大小。(我不知道该单件物品何时以客户身份完全移交给我)

Actually, the client can know when it has received a complete JSON object. When your client sees the } that matches the opening {, you have the complete object. Of course, this means that that your client needs to understand JSON syntax, but you can use an off-the-shelf JSON parser to do that.

实际上,客户端可以知道何时收到了一个完整的JSON对象。当您的客户端看到与开头的{匹配的}时,您就拥有了完整的对象。当然,这意味着您的客户机需要理解JSON语法,但是您可以使用现成的JSON解析器来实现这一点。

So the best way to do this is for the server to generate and send the JSON, and the client to parse the socket input stream using a normal JSON parser. If you do it that way, then you don't need to know whether the TCP/IP stack has broken the data stream into multiple packets. By the time the JSON parser sees them, they will have been reassembled into a stream of bytes.

因此,最好的方法是服务器生成并发送JSON,客户端使用普通的JSON解析器解析套接字输入流。如果您这样做,那么您不需要知道TCP/IP堆栈是否将数据流分解为多个数据包。当JSON解析器看到它们时,它们将被重新组合成一个字节流。


If this doesn't answer your question, we need to see what your code is currently doing to generate and send the JSON on the server side.

如果这没有回答您的问题,我们需要看看您的代码目前在做什么,以便在服务器端生成和发送JSON。

#1


0  

is there's a way to handle it so i don't have to regroup it because the limit size of tcp packet

有没有一种方法可以处理它,这样我就不必因为tcp包的限制大小而重新分组了

You don't have to care about the size of TCP packets. Just write the data. TCP will segmentize and packetize it for you.

您不必关心TCP包的大小。写数据。TCP将为您分段并打包它。

(cant know when the single object is fully transferred to me as client or not yet)

(不知道什么时候将单个对象作为客户端完全转移给我)

Yes you can. You reach the closing '}', as @StephenC mentions. Your JSON parser should take of that for you in any case.

是的,你可以。正如@StephenC提到的,你到达了关闭的“}”。在任何情况下,JSON解析器都应该为您提供。

Your question is founded on false assumptions.

你的问题是基于错误的假设。

#2


1  

If I have big object is there's a way to handle it so I don't have to regroup it because the limit size of tcp packet. (I cant know when the single object is fully transferred to me as client or not yet)

如果我有一个大对象,有一个方法可以处理它,所以我不需要重新分组因为tcp包的限制大小。(我不知道该单件物品何时以客户身份完全移交给我)

Actually, the client can know when it has received a complete JSON object. When your client sees the } that matches the opening {, you have the complete object. Of course, this means that that your client needs to understand JSON syntax, but you can use an off-the-shelf JSON parser to do that.

实际上,客户端可以知道何时收到了一个完整的JSON对象。当您的客户端看到与开头的{匹配的}时,您就拥有了完整的对象。当然,这意味着您的客户机需要理解JSON语法,但是您可以使用现成的JSON解析器来实现这一点。

So the best way to do this is for the server to generate and send the JSON, and the client to parse the socket input stream using a normal JSON parser. If you do it that way, then you don't need to know whether the TCP/IP stack has broken the data stream into multiple packets. By the time the JSON parser sees them, they will have been reassembled into a stream of bytes.

因此,最好的方法是服务器生成并发送JSON,客户端使用普通的JSON解析器解析套接字输入流。如果您这样做,那么您不需要知道TCP/IP堆栈是否将数据流分解为多个数据包。当JSON解析器看到它们时,它们将被重新组合成一个字节流。


If this doesn't answer your question, we need to see what your code is currently doing to generate and send the JSON on the server side.

如果这没有回答您的问题,我们需要看看您的代码目前在做什么,以便在服务器端生成和发送JSON。