From 7366e7815ffbd33d2ca048e393e2fba02d64a14c Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Mon, 8 Jun 2026 08:47:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/njcn/gather/detection/sntp/SntpPacketService.java | 4 +++- .../com/njcn/gather/detection/sntp/SntpPushMessage.java | 2 ++ .../com/njcn/gather/detection/sntp/SntpServerManager.java | 8 +++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPacketService.java b/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPacketService.java index 15f088a6..5b857282 100644 --- a/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPacketService.java +++ b/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPacketService.java @@ -15,6 +15,7 @@ public class SntpPacketService { private static final int MIN_PACKET_LENGTH = 48; private static final int CLIENT_MODE = 3; private static final int SERVER_MODE = 4; + // NTP纪元偏移量:2208988800秒(1900年到1970年的秒数差) private static final long NTP_EPOCH_OFFSET = 2208988800L; private static final ZoneId SHANGHAI_ZONE = ZoneId.of("Asia/Shanghai"); private static final byte[] REFERENCE_ID = "LOCL".getBytes(StandardCharsets.US_ASCII); @@ -54,11 +55,12 @@ public class SntpPacketService { return response; } - public SntpPushMessage toPushMessage(Instant computerInstant, Instant deviceInstant) { + public SntpPushMessage toPushMessage(String deviceIp, Instant computerInstant, Instant deviceInstant) { long computerTimestampMs = computerInstant.toEpochMilli(); long deviceTimestampMs = deviceInstant.toEpochMilli(); return new SntpPushMessage( "sntp_time_update", + deviceIp, formatShanghaiTime(computerInstant), formatShanghaiTime(deviceInstant), computerTimestampMs, diff --git a/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPushMessage.java b/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPushMessage.java index cf0a3e3c..f5d435c3 100644 --- a/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPushMessage.java +++ b/detection/src/main/java/com/njcn/gather/detection/sntp/SntpPushMessage.java @@ -11,6 +11,8 @@ public class SntpPushMessage { private String type = "sntp_time_update"; + private String deviceIp; + private String computerTime; private String deviceTime; diff --git a/detection/src/main/java/com/njcn/gather/detection/sntp/SntpServerManager.java b/detection/src/main/java/com/njcn/gather/detection/sntp/SntpServerManager.java index 97a7a657..552dc6be 100644 --- a/detection/src/main/java/com/njcn/gather/detection/sntp/SntpServerManager.java +++ b/detection/src/main/java/com/njcn/gather/detection/sntp/SntpServerManager.java @@ -118,6 +118,7 @@ public class SntpServerManager { } private void handlePacket(DatagramSocket socket, DatagramPacket packet) throws IOException { + Instant receiveInstant = Instant.now(); //T2:服务器接收请求时间 byte[] request = Arrays.copyOf(packet.getData(), packet.getLength()); InetSocketAddress clientAddress = new InetSocketAddress(packet.getAddress(), packet.getPort()); SntpExchange exchange = sntpPacketService.parseRequest(request, clientAddress); @@ -125,13 +126,14 @@ public class SntpServerManager { return; } - Instant receiveInstant = Instant.now(); - Instant transmitInstant = receiveInstant; + Instant transmitInstant = Instant.now(); //T3:服务器发送响应时间 byte[] response = sntpPacketService.buildResponse(exchange, receiveInstant, transmitInstant); DatagramPacket responsePacket = new DatagramPacket(response, response.length, packet.getAddress(), packet.getPort()); socket.send(responsePacket); - SntpPushMessage pushMessage = sntpPacketService.toPushMessage(transmitInstant, exchange.getDeviceInstant()); + String deviceIp = clientAddress.getAddress().getHostAddress(); +// SntpPushMessage pushMessage = sntpPacketService.toPushMessage(deviceIp, transmitInstant, exchange.getDeviceInstant()); + SntpPushMessage pushMessage = sntpPacketService.toPushMessage(deviceIp, receiveInstant, exchange.getDeviceInstant()); WebServiceManager.broadcast(JSON.toJSONString(pushMessage)); }