2015-10-12 23 views
0

我已經分配了在我的組織中執行TCP服務器來接收文本消息並將其拆分。但不幸的是,我的一些 消息字符變成垃圾(我已經使用JMeter作爲我的TCP客戶端)。我有兩個與這個問題有關的問題。任何幫助,高度讚賞。Netty TCP服務器字符變成垃圾

  1. 爲什麼我們不能使用「»」(u00BB)字符分割我的信息?它從來沒有工作,我們如何可以使用「»」作爲分隔符DelimiterBasedFrameDecoder

  2. 爲什麼我們會收到垃圾字符雖然我在編碼/解碼中使用了UTF-8? (僅管理接收郵件時,我的評論"pipeline.addLast("frameDecoder", new io.netty.handler.codec.DelimiterBasedFrameDecoder(500000, byteDeli)"

樣品要求:

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(); 
      } 

     } 

Handler類

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(); 
    } 
} 
+0

當您編寫'System.out.println(「«>」);'?Eclipse控制檯是否正確顯示字符? – Kayaman

+0

是的,我只是現在檢查它,它顯示正確的字符。 –

回答

1

發現了問題,它是不相關的網狀。 JMeter編碼出錯。在修改「jmeter.properties」屬性文件@ \ apache-jmeter-x.xx \ bin之後設法解決這個問題。

tcp.charset=UTF-8 

對不起,麻煩你們,因爲虛假與我同在。