MyClientNetty.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.jpsoft.education.netty;
  2. import com.jpsoft.education.netty.hander.MyClientHandler;
  3. import io.netty.bootstrap.Bootstrap;
  4. import io.netty.buffer.Unpooled;
  5. import io.netty.channel.*;
  6. import io.netty.channel.nio.NioEventLoopGroup;
  7. import io.netty.channel.socket.SocketChannel;
  8. import io.netty.channel.socket.nio.NioSocketChannel;
  9. import java.io.UnsupportedEncodingException;
  10. import java.util.concurrent.CountDownLatch;
  11. public class MyClientNetty {
  12. public static CountDownLatch countDownLatch = new CountDownLatch(1);
  13. public static CountDownLatch countDownLatch2 = new CountDownLatch(1);
  14. private String ip;
  15. private int port;
  16. private static ChannelFuture cf;
  17. private static EventLoopGroup bossGroup;
  18. public MyClientNetty(String ip, int port) {
  19. this.ip = ip;
  20. this.port = port;
  21. }
  22. public String sendRecv(String msg){
  23. try {
  24. cf.channel().writeAndFlush(Unpooled.copiedBuffer(msg.getBytes()));
  25. MyClientNetty.countDownLatch.await();
  26. return MyClientHandler.message;
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. return null;
  30. }
  31. }
  32. public void connect() throws UnsupportedEncodingException, InterruptedException {
  33. this.action();
  34. countDownLatch2.await();
  35. }
  36. public void close() throws InterruptedException {
  37. cf.channel().closeFuture().sync();
  38. bossGroup.shutdownGracefully();
  39. }
  40. public void action() throws InterruptedException, UnsupportedEncodingException {
  41. bossGroup = new NioEventLoopGroup();
  42. final Bootstrap bs = new Bootstrap();
  43. ((Bootstrap)((Bootstrap)((Bootstrap)((Bootstrap)bs.group(bossGroup)).channel(NioSocketChannel.class)).option(ChannelOption.SO_KEEPALIVE, true)).option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535))).handler(new ChannelInitializer<SocketChannel>() {
  44. protected void initChannel(SocketChannel socketChannel) throws Exception {
  45. socketChannel.pipeline().addLast(new ChannelHandler[]{new MyClientHandler()});
  46. }
  47. });
  48. (new Thread(new Runnable() {
  49. public void run() {
  50. try {
  51. MyClientNetty.cf = bs.connect(MyClientNetty.this.ip, MyClientNetty.this.port).sync();
  52. MyClientNetty.countDownLatch2.countDown();
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. })).start();
  58. }
  59. public static void main(String[] args) {
  60. try {
  61. MyClientNetty myClientNetty = new MyClientNetty("127.0.0.1",9999);
  62. myClientNetty.connect();
  63. String sendStr ="01 03 01 2c 00 0a";
  64. System.out.println("客户端发送数据:"+sendStr);
  65. String result = myClientNetty.sendRecv(sendStr);
  66. System.out.println("客户端获取返回数据:"+result);
  67. if(result.contains(" ")){
  68. result=result.replace(" ","");
  69. System.out.println("客户端获取返回处理:"+result);
  70. }
  71. myClientNetty.close();
  72. } catch (UnsupportedEncodingException e) {
  73. e.printStackTrace();
  74. } catch (InterruptedException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. }