Simple Socket Client doesn't read inputstream correctly.

104 views
Skip to first unread message

ivanics...@gmail.com

unread,
Apr 16, 2018, 4:30:59 AM4/16/18
to Netty discussions
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. 

Zhu Ran

unread,
May 17, 2018, 10:46:53 PM5/17/18
to Netty discussions
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.
Reply all
Reply to author
Forward
0 new messages