1. use netty in client side to make thing easier;

2. use JsonObjectDecoder in both server side and client side, don't use 
DelimiterBasedFrameDecoder;

3. don't add "\r\n" at the end of the json string  at both server side and 
client side when sending to the peer.



On Monday, April 16, 2018 at 4:30:59 PM UTC+8, ivanics...@gmail.com wrote:
>
> Hi,
>
> I made a simple Netty Server. It receives requests from clients, read 
> information from database and send it back to the client. The server send 
> back "the answer" in JSON format. The problem is that the JSON object 
> sometimes bigger than 15-20 MB. The server convert the object to JSON 
> format correctly, but the client doesn't get the whole information. 
> I always get the exception on the client side:
> com.google.gson.JsonSyntaxException: com.google.gson.stream.
> MalformedJsonException: Unterminated string at line 1 column 44021 path $.
> object
> The problem is that bufferedreader only reads the first 44020 characters 
> of the response. (However the response is much bigger.)
> Somebody can help me what is the problem with my code?
>
>
> My initializer class:
>     @Override
>     protected void initChannel(SocketChannel c) throws Exception {
>         ChannelPipeline pipeline = c.pipeline();
>         pipeline.addLast(new 
> DelimiterBasedFrameDecoder(Server.MAX_FRAME_SIZE, 
> Delimiters.lineDelimiter()));
>         pipeline.addLast(new StringDecoder(Charset.forName("UTF-8")));
>         pipeline.addLast(new StringEncoder(Charset.forName("UTF-8")));
>
>         pipeline.addLast("handler", new ServerHandler(p2p));
>     }
>
> Server.java sendMessage method:
>     private void sendMessage(ChannelHandlerContext ctx, Message msg, 
> boolean closeConnection) {
>         String msgJson = gson.toJson(msg) + "\r\n";
>         ChannelFuture future = ctx.writeAndFlush(msgJson);
>         if (closeConnection) {
>             future.addListener(ChannelFutureListener.CLOSE);
>         }
>     }
>
> Client.java sendRequestMessage
> public Message sendRequestMessage(Message msg) throws Exception {
>         Peer peer = getPeer();
>
>         Socket newClientSocket = new Socket(peer.getHost(), 
> peer.getPort());
>         InputStream bounded = new 
> BoundedInputStream(newClientSocket.getInputStream(), MAX_FRAME_SIZE);
>         BufferedReader reader = new BufferedReader(new 
> InputStreamReader(bounded));
>         PrintWriter writer = new 
> PrintWriter(newClientSocket.getOutputStream());
>
>         //Send request Message
>         writer.print(gson.toJson(msg, Message.class) + "\r\n");
>         writer.flush();
>
>         String response = reader.readLine();
>         
>         try {
>             newClientSocket.close();
>         } catch (Exception e) {
>             logger.warn("Can't close socket: " + peer.toString(), e);
>         }
>
>         
>         Message responseMessage = gson.fromJson(response, Message.class);
>         return responseMessage;
>
>     }
>
> I can't figure out what is the problem with my code. 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netty+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/netty/783bb97b-05ea-4e18-bc7b-29a3e0a64309%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to