i am working with Java and i am making a game. In this game the real time is very essensial part. For this reason i am trying to get the real time using Ntp.
What i found in the web is this code.
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class test{
public static void main(String args[]) throws Exception{
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
Date time = new Date(returnTime);
System.out.println("Time from " + TIME_SERVER + ": " + time);
}
}
My problem is that using this, the system is still getting the System.currentMillis(); because it actually prints you the time that your local machine got the time message. For this reason if the player changes his Desktop time to the future, the game time will also change.
If someone could help in order to have the real time i would be very thankful. P.S. i dont have a server, my game will be playing on Kongregate and the data will be on the players computer.
Thanks in advance.
getReturnTime
does not give you the time of the server it gives you the time at which time message packet was received by local machine. (According to the javadoc). – HydrastisgetDelay()
andgetOffset()
methods are for? – Nonflammable