본문 바로가기

java framework/netty

hexString 을 byte로 변환해주는 netty 서버 테스트

  1. Jmeter 설정

% Jmeter 설치 및 설정은 https://man-tae.tistory.com/9 참고

1.1 Jmeter 실행

Jmeter 실행 -> Thread Group 생성

1.2 TCP Sampler 생성

TCP Sampler 생성->

TCPClient classname 에 BinaryTCPClientImpl 입력 -> 전송할 IP, 포터 번호 입력 -> 전송할 hexString 입력(공백이 없어야 함)

1.3 listener 생성

원하는 listener 생성

  1. Netty 서버 생성

2.1 netty project 생성

이클립스 실행 -> 메이븐 프로젝트 생성 -> hServer.java, hServeHandler.java 생성 -> pom.xml에 netty dependency 추가

2.2 코드 작성

hServer.java 작성

  package [com.hexTo.string.hexToString;](com.hexTo.string.hexToString;)  

  import [io.netty.bootstrap.ServerBootstrap;](io.netty.bootstrap.ServerBootstrap;)  
  import [io.netty.channel.ChannelFuture;](io.netty.channel.ChannelFuture;)  
  import [io.netty.channel.ChannelInitializer;](io.netty.channel.ChannelInitializer;)  
  import [io.netty.channel.ChannelPipeline;](io.netty.channel.ChannelPipeline;)  
  import [io.netty.channel.EventLoopGroup;](io.netty.channel.EventLoopGroup;)  
  import [io.netty.channel.nio.NioEventLoopGroup;](io.netty.channel.nio.NioEventLoopGroup;)  
  import [io.netty.channel.socket.SocketChannel;](io.netty.channel.socket.SocketChannel;)  
  import [io.netty.channel.socket.nio.NioServerSocketChannel;](io.netty.channel.socket.nio.NioServerSocketChannel;)  
  import [io.netty.handler.codec.string.StringDecoder;](io.netty.handler.codec.string.StringDecoder;)  

  public class hServer {  
      public static void main(String\[\] args) throws Exception {  
          EventLoopGroup bossGroup = new NioEventLoopGroup(1);  
          EventLoopGroup workerGroup = new NioEventLoopGroup();  
          try {  
              ServerBootstrap b = new ServerBootstrap();  
              b.group(bossGroup, workerGroup)  
               .channel([NioServerSocketChannel.class)](NioServerSocketChannel.class))  
               .childHandler(new ChannelInitializer() {  
                  @Override  
                  public void initChannel(SocketChannel ch) {  
                      ChannelPipeline p = ch.pipeline();  
                      p.addLast(new hServerHandler());  

                  }  
              });  
              ChannelFuture f = b.bind(8888).sync();  
              f.channel().closeFuture().sync();  
          }  
          finally {  
              workerGroup.shutdownGracefully();  
              bossGroup.shutdownGracefully();  
          }  
      }  
  }

hServeHandler.java 작성

  package [com.hexTo.string.hexToString;](com.hexTo.string.hexToString;)  

  import [io.netty.buffer.ByteBuf;](io.netty.buffer.ByteBuf;)  
  import [io.netty.buffer.ByteBufUtil;](io.netty.buffer.ByteBufUtil;)  
  import [io.netty.channel.ChannelHandlerContext;](io.netty.channel.ChannelHandlerContext;)  
  import [io.netty.channel.ChannelInboundHandlerAdapter;](io.netty.channel.ChannelInboundHandlerAdapter;)  

  public class hServerHandler extends ChannelInboundHandlerAdapter {  

  public static byte\[\] hexStringToByte(String s) {  
      int len = s.length();  
      byte\[\] data = new byte\[len / 2\];  
      for (int i = 0; i < len; i += 2) {  
          data\[i / 2\] = (byte) (([Character.digit(s.charAt(i),](Character.digit(s.charAt(i),) 16) << 4)  
                               + [Character.digit(s.charAt(i+1),](Character.digit(s.charAt(i+1),) 16));  
      }  
      return data;  
  }  

  public static String byteToHexString(byte\[\] bytes){   

  StringBuilder sb = new StringBuilder();   

  for(byte b : bytes){   

  [sb.append(String.format(](sb.append(String.format()"%02X", b&0xff));   
  }   

  return sb.toString();   
  }   

  @Override  
      public void channelRead(ChannelHandlerContext ctx, Object msg) {  
          ByteBuf readMessage = (ByteBuf) msg;  
          String bbu = ByteBufUtil.hexDump(readMessage);          
          [System.out.println(](System.out.println()"channelRead : hexdump; " + bbu);  

          byte\[\] ByteArray = hexStringToByte(bbu);  
          [System.out.println(](System.out.println()"channelRead : hex to byte; " + ByteArray.toString());  

          String str = byteToHexString(ByteArray);  
          [System.out.println(](System.out.println()"channelRead : byte to hex; " + str);  

          [ctx.write(ByteArray);](ctx.write(ByteArray);)         

      }  

      @Override  
      public void channelReadComplete(ChannelHandlerContext ctx) {  
          [System.out.println(](System.out.println()"channelReadComplete");  
          [ctx.flush();](ctx.flush();)  
          [ctx.close();](ctx.close();)  
      }  

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

Netty 서버 실행 -> Jmeter 데이터 전송 -> 데이터 전송 확인