package com.mes.test; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil; import java.util.Scanner; import java.util.concurrent.TimeUnit; public class YgslConnectionTest { private static final int PORT = 4545; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("========================================"); System.out.println(" 扭力枪连接测试工具"); System.out.println("========================================"); System.out.println("请选择要测试的枪:"); System.out.println("1. A枪 (192.168.100.121)"); System.out.println("2. B枪 (192.168.100.122)"); System.out.println("3. C枪 (192.168.100.123)"); System.out.println("4. D枪 (192.168.100.124)"); System.out.println("5. 测试所有枪"); System.out.println("0. 退出"); System.out.println("========================================"); while (true) { System.out.print("\n请输入选项: "); String input = scanner.nextLine().trim(); switch (input) { case "1": testConnection("A枪", "192.168.100.121"); break; case "2": testConnection("B枪", "192.168.100.122"); break; case "3": testConnection("C枪", "192.168.100.123"); break; case "4": testConnection("D枪", "192.168.100.124"); break; case "5": testAllConnections(); break; case "0": System.out.println("退出测试程序"); scanner.close(); System.exit(0); break; default: System.out.println("无效选项,请重新输入"); } } } private static void testConnection(String gunName, String ip) { System.out.println("\n========================================"); System.out.println("开始测试 " + gunName + " (" + ip + ":" + PORT + ")"); System.out.println("========================================"); EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); try { bootstrap.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) .handler(new ChannelInitializer() { protected void initChannel(SocketChannel socketChannel) throws Exception { ByteBuf delimiter = Unpooled.copiedBuffer(new byte[]{0x00}); socketChannel.pipeline() .addLast(new DelimiterBasedFrameDecoder(1024, delimiter)) .addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)) .addLast(new SimpleChannelInboundHandler() { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) { System.out.println(gunName + " 收到数据: " + msg); } @Override public void channelActive(ChannelHandlerContext ctx) { System.out.println(gunName + " 连接成功!通道已激活"); System.out.println("本地地址: " + ctx.channel().localAddress()); System.out.println("远程地址: " + ctx.channel().remoteAddress()); } @Override public void channelInactive(ChannelHandlerContext ctx) { System.out.println(gunName + " 连接断开"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { System.out.println(gunName + " 发生异常: " + cause.getMessage()); cause.printStackTrace(); } }); } }) .remoteAddress(ip, PORT); ChannelFuture future = bootstrap.connect().sync(); System.out.println(gunName + " TCP连接建立成功"); System.out.println("\n按回车键断开连接..."); System.in.read(); future.channel().close().sync(); System.out.println(gunName + " 连接已关闭"); } catch (Exception e) { System.out.println(gunName + " 连接失败: " + e.getMessage()); e.printStackTrace(); } finally { group.shutdownGracefully(); System.out.println(gunName + " 测试完成"); } } private static void testAllConnections() { System.out.println("\n========================================"); System.out.println("开始测试所有枪的连接"); System.out.println("========================================"); String[] guns = {"A枪", "B枪", "C枪", "D枪"}; String[] ips = {"192.168.100.121", "192.168.100.122", "192.168.100.123", "192.168.100.124"}; for (int i = 0; i < guns.length; i++) { System.out.println("\n--- 测试 " + guns[i] + " (" + ips[i] + ") ---"); boolean connected = quickTestConnection(ips[i]); if (connected) { System.out.println("✓ " + guns[i] + " 连接成功"); } else { System.out.println("✗ " + guns[i] + " 连接失败"); } } System.out.println("\n========================================"); System.out.println("所有枪测试完成"); System.out.println("========================================"); } private static boolean quickTestConnection(String ip) { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); try { bootstrap.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000) .handler(new ChannelInitializer() { protected void initChannel(SocketChannel socketChannel) throws Exception { ByteBuf delimiter = Unpooled.copiedBuffer(new byte[]{0x00}); socketChannel.pipeline() .addLast(new DelimiterBasedFrameDecoder(1024, delimiter)) .addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)) .addLast(new SimpleChannelInboundHandler() { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) { } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { } }); } }) .remoteAddress(ip, PORT); ChannelFuture future = bootstrap.connect(); if (future.isSuccess()) { future.channel().close().sync(); return true; } return false; } catch (Exception e) { System.out.println("连接异常: " + e.getMessage()); return false; } finally { group.shutdownGracefully(); } } }