Netty TCP服务器字符变成垃圾

时间:2015-10-12 10:32:41

标签: java tcp netty

我已经分配在我的组织中执行TCP服务器以接收文本消息并将其拆分。但不幸的是我的一些 消息字符变成垃圾(我已经使用JMeter作为我的TCP客户端)。我有2个与此问题相关的问题。任何帮助都非常感谢。

  
      
  1. 为什么我们不能使用“»”(u00BB)字符拆分我的信息?它从来没有用过,我们如何在DelimiterBasedFrameDecoder中使用“»”作为分隔符?

  2.   
  3. 为什么我们收到垃圾字符虽然我在编码/解码中使用了UTF-8? (仅在我评论"pipeline.addLast("frameDecoder", new io.netty.handler.codec.DelimiterBasedFrameDecoder( 500000, byteDeli)"

  4. 时设法接收消息   

样品申请:

pov1‹1‹202030‹81056581‹0‹6‹565810000011‹0‹130418135639‹3‹4‹0‹cha7373737›chaE15E2512380›1›1«ban7373737›banE15E2512380›2›2«ind7373737›indE15E2512380›3›3»
Eclipse cosole:收到请求::::::

pov1�1�202030�81056581�0�6�565810000011�0�130418135639�3�4�0�cha7373737�chaE15E2512380�1�1�ban7373737�banE15E2512380�2�2�ind7373737�indE15E2512380�3�3�

服务器类: -

public void run() {
            try {
                System.out.println("2:run");
                bootstrap
                        .group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch)
                                    throws Exception {
                                ChannelPipeline pipeline = ch.pipeline();
                                DTMTCPServiceHandler serviceHandler = context
                                        .getBean(DTMTCPServiceHandler.class);
                                pipeline.addFirst(new LoggingHandler(
                                        LogLevel.INFO));

                                byte[] delimiter = "\u00BB".getBytes(CharsetUtil.UTF_8);//»
                                ByteBuf byteDeli = Unpooled.copiedBuffer(delimiter);                                

                                pipeline.addLast(
                                        "frameDecoder",
                                        new io.netty.handler.codec.DelimiterBasedFrameDecoder(
                                                500000, byteDeli)); // Decoders
                                pipeline.addLast("stringDecoder",
                                        new StringDecoder(CharsetUtil.UTF_8));
                                pipeline.addLast("stringEncoder",
                                        new StringEncoder(CharsetUtil.UTF_8));
                                pipeline.addLast("messageHandler",
                                        serviceHandler);

                            }
                        }).option(ChannelOption.SO_BACKLOG, 128)
                        .childOption(ChannelOption.SO_KEEPALIVE, true);
                serverChannel = bootstrap.bind(7070).sync().channel()
                        .closeFuture().sync().channel();
            } catch (InterruptedException e) {
                //error
                logger.error("POSGatewayServiceThread : InterruptedException",
                        e);
                System.out.println(e);
            } finally {
                //finally
                System.out.println("finally");
                serverChannel.close();
                workerGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }

        }

处理程序类

public class DTMTCPServiceHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        String posMessage = msg.toString();

        System.out.println("Recieved Request :::::: " + posMessage);
        String response = "-";
        ByteBuf copy = null;
        try {
            //Called to separate splitter class
            response = dtmtcpServiceManager.manageDTMTCPMessage(posMessage);
            copy = Unpooled.copiedBuffer(response.getBytes());

        } finally {
            logger.info("Recieved Response :::::: " + response);
            ctx.write(copy);
            ctx.flush();
        }

    }       

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //Open
        super.channelActive(ctx);
    }       

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        //End
        super.channelReadComplete(ctx);
    }

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

1 个答案:

答案 0 :(得分:1)

发现问题并且与netty无关。错误是使用JMeter编码。设法在修改&#34; jmeter.properties&#34;之后解决了这个问题。属性文件@ \ apache-jmeter-x.xx \ bin。

tcp.charset=UTF-8

很抱歉给你们带来麻烦,因为对我不好。