| | |
| | | import java.net.DatagramPacket; |
| | | import java.net.DatagramSocket; |
| | | import java.net.InetAddress; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.atomic.AtomicLong; |
| | |
| | | |
| | | public class UDPPortAReceiver { |
| | | public static final int PORT = 8234; |
| | | @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 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()); |
| | | packetCount.incrementAndGet(); |
| | | // 获取当前时间和时间戳 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String time = sdf.format(new Date()); |
| | | int timestamp = (int)(System.currentTimeMillis() / 1000); |
| | | |
| | | // 保存到HexBaowen集合 |
| | | // 调用时添加本地端口参数 |
| | | HexBaowen.intsert(ip, hexData, time, port, LOCAL_PORT, timestamp); |
| | | 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()); |