简体   繁体   中英

Get date from Netty SocketServer

I have a simple socket server created via Netty.io and when I write something from terminal, everything works like a charm. but I also have a custom socket client, without using netty.io, and how I can get data from netty server? this is my netty handler:

public class DiscardServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
        String msg = message;
        try {
            System.out.println(msg);
            System.out.flush();
            ctx.writeAndFlush("Response from server");
        } finally {
            ReferenceCountUtil.retain(msg);
        }
    }

}

and this is my client socket class:

public class ClientSideSocket2 {
    public static void main(String[] args) {
        String serverName = "localhost";
        int port = 9999;
        String line = "";
        Socket client = null;
        PrintWriter toServer = null;
        BufferedReader fromServer = null;

        try {
            System.out.println("Connecting to " + serverName + " on port " + port);
            client = new Socket(serverName, port);
            System.out.println("Just connected to " + client.getRemoteSocketAddress());

            toServer = new PrintWriter(client.getOutputStream(), true);
            fromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));

            for (int i = 0; i < 6; i++) {
                toServer.println("simple string: " + i);
                if ((line = fromServer.readLine()) != null) {
                    System.out.println("Responce from server: " + line);
                }
            }           

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                toServer.close();
                fromServer.close();
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

i can't read data from the line if ((line = fromServer.readLine()) != null) . is there another way to do it?

Firstly, I am assume that you setup the ChannelPipeline properly and the channelRead0 is triggered. fromServer.readLine() get blocked util it read a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed.

Then the problem is you should response

ctx.writeAndFlush("Response from server\n");

If the event doesn't trigger, try setup your ChannelPipeline this way:

ch.pipeline().addLast(
    new StringDecoder(),
    new StringEncoder(),
    new DiscardServerHandler());

Then terminated the responsed string, your client will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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