| | |
| | | import java.util.concurrent.atomic.AtomicLong; |
| | | |
| | | import dell_system.MessageViewPanel; |
| | | import dell_targets.Dell_SystemConfiguration; |
| | | |
| | | public class UDPPortAReceiver { |
| | | public static final int PORT = 8234; |
| | | @SuppressWarnings("unused") |
| | | public static final int PORT =Dell_SystemConfiguration.hexport; |
| | | @SuppressWarnings("unused") |
| | | private static final int MAX_DEVICES = 50000; |
| | | private static final AtomicLong packetCount = new AtomicLong(0); |
| | | private static final ExecutorService executor = Executors.newFixedThreadPool(10); |
| | | private static DatagramSocket socket; |
| | | private static volatile boolean isRunning = false; |
| | | private static Thread receiverThread; |
| | | @SuppressWarnings("unused") |
| | | private static final AtomicLong packetCount = new AtomicLong(0); |
| | | private static final ExecutorService executor = Executors.newFixedThreadPool(10); |
| | | private static DatagramSocket socket; |
| | | private static volatile boolean isRunning = false; |
| | | private static Thread receiverThread; |
| | | @SuppressWarnings("unused") |
| | | private static final int LOCAL_PORT = PORT; // 定义本地端口 |
| | | // 启动接收器的静态方法 |
| | | public static void startReceiver() { |
| | | if (isRunning) return; |
| | | isRunning = true; |
| | | receiverThread = new Thread(() -> { |
| | | try { |
| | | socket = new DatagramSocket(PORT); |
| | | byte[] buffer = new byte[2048]; |
| | | while (isRunning) { |
| | | DatagramPacket packet = new DatagramPacket(buffer, buffer.length); |
| | | socket.receive(packet); |
| | | executor.execute(() -> { |
| | | try { |
| | | String ip = packet.getAddress().getHostAddress(); |
| | | int port = packet.getPort(); |
| | | String hexData = bytesToHex(packet.getData(), packet.getLength()); |
| | | // 调用时添加本地端口参数 |
| | | PacketProcessingSystem.storePacket(ip, port, hexData); |
| | | // 报文查看窗口显示数据 |
| | | MessageViewPanel.showData(hexData, ip, port, PORT,"1"); |
| | | |
| | | } catch (Exception e) { |
| | | System.err.println("Error processing UDP-A packet: " + e.getMessage()); |
| | | } |
| | | }); |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("UDP-A Server crashed: " + e.getMessage()); |
| | | } finally { |
| | | if (socket != null && !socket.isClosed()) { |
| | | socket.close(); |
| | | } |
| | | } |
| | | }); |
| | | receiverThread.setDaemon(true); |
| | | receiverThread.start(); |
| | | } |
| | | |
| | | // 停止接收器 |
| | | public static void stopReceiver() { |
| | | isRunning = false; |
| | | if (socket != null && !socket.isClosed()) { |
| | | socket.close(); |
| | | } |
| | | executor.shutdown(); |
| | | } |
| | | |
| | | // 发送数据到指定设备 |
| | | public static void sendData(String ip, int port, String data, boolean isHex) { |
| | | try { |
| | | byte[] sendBytes; |
| | | if (isHex) { |
| | | // HEX格式发送:将十六进制字符串转换为字节数组 |
| | | sendBytes = hexStringToByteArray(data); |
| | | } else { |
| | | // ASCII格式发送:直接获取字节数组 |
| | | sendBytes = data.getBytes(); |
| | | } |
| | | |
| | | InetAddress address = InetAddress.getByName(ip); |
| | | DatagramPacket packet = new DatagramPacket(sendBytes, sendBytes.length, address, port); |
| | | socket.send(packet); |
| | | } catch (Exception e) { |
| | | System.err.println("Error sending UDP data: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private static String bytesToHex(byte[] bytes, int length) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (int i = 0; i < length; i++) { |
| | | sb.append(String.format("%02X", bytes[i])); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private static byte[] hexStringToByteArray(String hex) { |
| | | hex = hex.replaceAll("\\s", ""); // 移除所有空格 |
| | | int len = hex.length(); |
| | | byte[] data = new byte[len / 2]; |
| | | for (int i = 0; i < len; i += 2) { |
| | | // 处理每两个字符作为一个字节 |
| | | data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) |
| | | + Character.digit(hex.charAt(i+1), 16)); |
| | | } |
| | | return data; |
| | | } |
| | | // 启动接收器的静态方法 |
| | | public static void startReceiver() { |
| | | if (isRunning) return; |
| | | isRunning = true; |
| | | receiverThread = new Thread(() -> { |
| | | try { |
| | | socket = new DatagramSocket(PORT); |
| | | byte[] buffer = new byte[2048]; |
| | | while (isRunning) { |
| | | DatagramPacket packet = new DatagramPacket(buffer, buffer.length); |
| | | socket.receive(packet); |
| | | executor.execute(() -> { |
| | | try { |
| | | String ip = packet.getAddress().getHostAddress(); |
| | | int port = packet.getPort(); |
| | | String hexData = bytesToHex(packet.getData(), packet.getLength()); |
| | | // 调用时添加本地端口参数 |
| | | PacketProcessingSystem.storePacket(ip, port, hexData); |
| | | // 报文查看窗口显示数据 |
| | | MessageViewPanel.showData(hexData, ip, port, PORT,"1"); |
| | | // +++ 增加包计数 +++ |
| | | packetCount.incrementAndGet(); // 关键修复:增加计数器 |
| | | } catch (Exception e) { |
| | | System.err.println("Error processing UDP-A packet: " + e.getMessage()); |
| | | } |
| | | }); |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("UDP-A Server crashed: " + e.getMessage()); |
| | | } finally { |
| | | if (socket != null && !socket.isClosed()) { |
| | | socket.close(); |
| | | } |
| | | } |
| | | }); |
| | | receiverThread.setDaemon(true); |
| | | receiverThread.start(); |
| | | } |
| | | |
| | | public static long getPacketCount() { |
| | | return packetCount.get(); |
| | | } |
| | | // 停止接收器 |
| | | public static void stopReceiver() { |
| | | isRunning = false; |
| | | if (socket != null && !socket.isClosed()) { |
| | | socket.close(); |
| | | } |
| | | executor.shutdown(); |
| | | } |
| | | |
| | | // 发送数据到指定设备 |
| | | public static void sendData(String ip, int port, String data, boolean isHex) { |
| | | try { |
| | | byte[] sendBytes; |
| | | if (isHex) { |
| | | // HEX格式发送:将十六进制字符串转换为字节数组 |
| | | sendBytes = hexStringToByteArray(data); |
| | | } else { |
| | | // ASCII格式发送:直接获取字节数组 |
| | | sendBytes = data.getBytes(); |
| | | } |
| | | |
| | | InetAddress address = InetAddress.getByName(ip); |
| | | DatagramPacket packet = new DatagramPacket(sendBytes, sendBytes.length, address, port); |
| | | socket.send(packet); |
| | | } catch (Exception e) { |
| | | System.err.println("Error sending UDP data: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private static String bytesToHex(byte[] bytes, int length) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (int i = 0; i < length; i++) { |
| | | sb.append(String.format("%02X", bytes[i])); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private static byte[] hexStringToByteArray(String hex) { |
| | | hex = hex.replaceAll("\\s", ""); // 移除所有空格 |
| | | int len = hex.length(); |
| | | byte[] data = new byte[len / 2]; |
| | | for (int i = 0; i < len; i += 2) { |
| | | // 处理每两个字符作为一个字节 |
| | | data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) |
| | | + Character.digit(hex.charAt(i+1), 16)); |
| | | } |
| | | return data; |
| | | } |
| | | |
| | | // 获取接收到的数据包总数 |
| | | public static long getPacketCount() { |
| | | return packetCount.get(); |
| | | } |
| | | |
| | | } |