克里斯蒂安&x2019;s算法java同步时间错误
我正在用java开发一个套接字服务器/客户端。客户机发送请求请求时间。服务器发送响应。同步客户端时间是使用Cristian算法计算的。Tclient=Tserver+(T1-T0)/2 服务器代码:克里斯蒂安&x2019;s算法java同步时间错误,java,algorithm,Java,Algorithm,我正在用java开发一个套接字服务器/客户端。客户机发送请求请求时间。服务器发送响应。同步客户端时间是使用Cristian算法计算的。Tclient=Tserver+(T1-T0)/2 服务器代码: @Override public void run() { String received; while (true) { try { // Ask user what he wants dos.writeUTF("
@Override
public void run() {
String received;
while (true) {
try {
// Ask user what he wants
dos.writeUTF("Type 't' to get time..\n" +
"Type exit to terminate connection.");
// receive the answer from client
received = dis.readUTF();
if (received.equals("exit")) {
System.out.println("Client " + this.s + " sends exit...");
System.out.println("Closing this connection.");
this.s.close();
System.out.println("Connection closed");
break;
} else if (received.equals("t")) {
long currentTimeMillis = Instant.now().toEpochMilli();
dos.writeLong(currentTimeMillis);
String ip=(((InetSocketAddress) s.getRemoteSocketAddress()).getAddress()).toString().replace("/","");
insert(ip, currentTimeMillis);
} else {
dos.writeUTF("Invalid input");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端代码:
try (Scanner scn = new Scanner(System.in);
Socket s = new Socket(InetAddress.getByName("localhost"), 5068);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream())) {
// the following loop performs the exchange of
// information between client and client handler
while (true) {
System.out.println(dis.readUTF());
String tosend = scn.nextLine();
dos.writeUTF(tosend);
//Time when the request is sent
Instant requestTimeInstant = Instant.now();
System.out.println("Time at which the request is sent " + getFormattedTime(requestTimeInstant.toEpochMilli()));
// If client sends exit,close this connection
// and then break from the while loop
if (tosend.equals("exit")) {
System.out.println("Closing this connection : " + s);
s.close();
System.out.println("Connection closed");
break;
} else if(tosend.equals("t")){
// printing date or time as requested by client
long serverTime = dis.readLong();
Instant responseTimeInstant = Instant.now();
System.out.println("Time at which the response is received Instant " + getFormattedTime(responseTimeInstant.toEpochMilli()));
//Apply christian' algorithm Tclient = Tserver + (T1 - T0)/2 where
// T0 time at which the request is sent
//and T1 time at which the response is received
long deltaMill = Duration.between(requestTimeInstant,responseTimeInstant).toMillis();
long halfDeltaTime = deltaMill / 2;
System.out.println(deltaMill);
System.out.println("Added time to serverTime " + halfDeltaTime + " Milliseconds");
long clientTimeMills = serverTime + halfDeltaTime;
System.out.println("Server time " + getFormattedTime(serverTime));
System.out.println("client time " + getFormattedTime(clientTimeMills));
System.out.println("Sync time error " +(responseTimeInstant.toEpochMilli() -clientTimeMills) + " Milliseconds");
}
}
} catch (Exception e) {
e.printStackTrace();
}
我实际上有一个不连贯的输出(仅在请求的第一个响应中,存在同步时间错误),我无法找出原因: