YgslConnectionTest.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.mes.test;
  2. import io.netty.bootstrap.Bootstrap;
  3. import io.netty.buffer.ByteBuf;
  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 io.netty.handler.codec.DelimiterBasedFrameDecoder;
  10. import io.netty.handler.codec.string.StringEncoder;
  11. import io.netty.util.CharsetUtil;
  12. import java.util.Scanner;
  13. import java.util.concurrent.TimeUnit;
  14. public class YgslConnectionTest {
  15. private static final int PORT = 4545;
  16. public static void main(String[] args) {
  17. Scanner scanner = new Scanner(System.in);
  18. System.out.println("========================================");
  19. System.out.println(" 扭力枪连接测试工具");
  20. System.out.println("========================================");
  21. System.out.println("请选择要测试的枪:");
  22. System.out.println("1. A枪 (192.168.100.121)");
  23. System.out.println("2. B枪 (192.168.100.122)");
  24. System.out.println("3. C枪 (192.168.100.123)");
  25. System.out.println("4. D枪 (192.168.100.124)");
  26. System.out.println("5. 测试所有枪");
  27. System.out.println("0. 退出");
  28. System.out.println("========================================");
  29. while (true) {
  30. System.out.print("\n请输入选项: ");
  31. String input = scanner.nextLine().trim();
  32. switch (input) {
  33. case "1":
  34. testConnection("A枪", "192.168.100.121");
  35. break;
  36. case "2":
  37. testConnection("B枪", "192.168.100.122");
  38. break;
  39. case "3":
  40. testConnection("C枪", "192.168.100.123");
  41. break;
  42. case "4":
  43. testConnection("D枪", "192.168.100.124");
  44. break;
  45. case "5":
  46. testAllConnections();
  47. break;
  48. case "0":
  49. System.out.println("退出测试程序");
  50. scanner.close();
  51. System.exit(0);
  52. break;
  53. default:
  54. System.out.println("无效选项,请重新输入");
  55. }
  56. }
  57. }
  58. private static void testConnection(String gunName, String ip) {
  59. System.out.println("\n========================================");
  60. System.out.println("开始测试 " + gunName + " (" + ip + ":" + PORT + ")");
  61. System.out.println("========================================");
  62. EventLoopGroup group = new NioEventLoopGroup();
  63. Bootstrap bootstrap = new Bootstrap();
  64. try {
  65. bootstrap.group(group)
  66. .channel(NioSocketChannel.class)
  67. .option(ChannelOption.TCP_NODELAY, true)
  68. .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
  69. .handler(new ChannelInitializer<SocketChannel>() {
  70. protected void initChannel(SocketChannel socketChannel) throws Exception {
  71. ByteBuf delimiter = Unpooled.copiedBuffer(new byte[]{0x00});
  72. socketChannel.pipeline()
  73. .addLast(new DelimiterBasedFrameDecoder(1024, delimiter))
  74. .addLast("encoder", new StringEncoder(CharsetUtil.UTF_8))
  75. .addLast(new SimpleChannelInboundHandler<String>() {
  76. @Override
  77. protected void channelRead0(ChannelHandlerContext ctx, String msg) {
  78. System.out.println(gunName + " 收到数据: " + msg);
  79. }
  80. @Override
  81. public void channelActive(ChannelHandlerContext ctx) {
  82. System.out.println(gunName + " 连接成功!通道已激活");
  83. System.out.println("本地地址: " + ctx.channel().localAddress());
  84. System.out.println("远程地址: " + ctx.channel().remoteAddress());
  85. }
  86. @Override
  87. public void channelInactive(ChannelHandlerContext ctx) {
  88. System.out.println(gunName + " 连接断开");
  89. }
  90. @Override
  91. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  92. System.out.println(gunName + " 发生异常: " + cause.getMessage());
  93. cause.printStackTrace();
  94. }
  95. });
  96. }
  97. })
  98. .remoteAddress(ip, PORT);
  99. ChannelFuture future = bootstrap.connect().sync();
  100. System.out.println(gunName + " TCP连接建立成功");
  101. System.out.println("\n按回车键断开连接...");
  102. System.in.read();
  103. future.channel().close().sync();
  104. System.out.println(gunName + " 连接已关闭");
  105. } catch (Exception e) {
  106. System.out.println(gunName + " 连接失败: " + e.getMessage());
  107. e.printStackTrace();
  108. } finally {
  109. group.shutdownGracefully();
  110. System.out.println(gunName + " 测试完成");
  111. }
  112. }
  113. private static void testAllConnections() {
  114. System.out.println("\n========================================");
  115. System.out.println("开始测试所有枪的连接");
  116. System.out.println("========================================");
  117. String[] guns = {"A枪", "B枪", "C枪", "D枪"};
  118. String[] ips = {"192.168.100.121", "192.168.100.122", "192.168.100.123", "192.168.100.124"};
  119. for (int i = 0; i < guns.length; i++) {
  120. System.out.println("\n--- 测试 " + guns[i] + " (" + ips[i] + ") ---");
  121. boolean connected = quickTestConnection(ips[i]);
  122. if (connected) {
  123. System.out.println("✓ " + guns[i] + " 连接成功");
  124. } else {
  125. System.out.println("✗ " + guns[i] + " 连接失败");
  126. }
  127. }
  128. System.out.println("\n========================================");
  129. System.out.println("所有枪测试完成");
  130. System.out.println("========================================");
  131. }
  132. private static boolean quickTestConnection(String ip) {
  133. EventLoopGroup group = new NioEventLoopGroup();
  134. Bootstrap bootstrap = new Bootstrap();
  135. try {
  136. bootstrap.group(group)
  137. .channel(NioSocketChannel.class)
  138. .option(ChannelOption.TCP_NODELAY, true)
  139. .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
  140. .handler(new ChannelInitializer<SocketChannel>() {
  141. protected void initChannel(SocketChannel socketChannel) throws Exception {
  142. ByteBuf delimiter = Unpooled.copiedBuffer(new byte[]{0x00});
  143. socketChannel.pipeline()
  144. .addLast(new DelimiterBasedFrameDecoder(1024, delimiter))
  145. .addLast("encoder", new StringEncoder(CharsetUtil.UTF_8))
  146. .addLast(new SimpleChannelInboundHandler<String>() {
  147. @Override
  148. protected void channelRead0(ChannelHandlerContext ctx, String msg) {
  149. }
  150. @Override
  151. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  152. }
  153. });
  154. }
  155. })
  156. .remoteAddress(ip, PORT);
  157. ChannelFuture future = bootstrap.connect();
  158. if (future.isSuccess()) {
  159. future.channel().close().sync();
  160. return true;
  161. }
  162. return false;
  163. } catch (Exception e) {
  164. System.out.println("连接异常: " + e.getMessage());
  165. return false;
  166. } finally {
  167. group.shutdownGracefully();
  168. }
  169. }
  170. }