服务器与客户端时钟同步

发布于 2025-01-03 21:42:37 字数 379 浏览 1 评论 0原文

在我的应用程序中,我有一个服务器和 x 个客户端。当Client启动时,他从Server获取当前的系统时间。每个客户端都必须使用服务器时间,不能使用自己的系统时间。

现在我的问题:在客户端上运行时钟的最佳方法是什么?该时钟从当前服务器时间开始,并几乎与其同步运行,而不需要每 x 秒接收一次服务器时间?

目标是在客户端上显示运行时钟以及服务器时间。

客户端时钟可能具有的容差约为 24 小时内 1 秒。

在我的解决方案中,我得到了一个每 500 毫秒触发一次的计时器,并在计时器执行时对服务器时间进行 500 毫秒计数。但这不是一个好的解决方案:)因为客户端时钟与服务器时间不同。

感谢您的回复

In my application I have a Server and x Clients. When a Client starts, he obtained the current System time from the Server. Every Client has to work with the Server time and can´t use his own System time.

Now my Question: What is the best way to run a clock on the Client that starts with the current Server time and run nearly synchronous to it without to receive the Server time every x seconds?

The goal is to display a runing clock with the Server time on the client.

The tolerance that the client clock may have is about 1second in 24hours.

In my solution I got a Timer that trigger every 500ms and count 500ms on the Server time when the Timer executes. But this is not a good solution :) because the Client clock differ from the Server time.

Thanks for your reply

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对风讲故事 2025-01-10 21:42:37

您几乎肯定应该使用已建立的时钟同步方法,例如网络时间协议,而不是构建自己的时钟同步方法定制解决方案。它将为您提供比您自己获得的更好的结果,并且您还有一个额外的好处,即所有服务器都同意现在的时间:-)

You should almost certainly use an established clock synchronization method such as the Network Time Protocol rather than building your own custom solution. It will provide you with better results than you will make yourself, and you have the added benefit that all your servers agree about what time it is :-)

野の 2025-01-10 21:42:37

我找到了一个完全适合我的情况的解决方案。
我没有使用 System.currentTimeMillis(),而是使用 System.nanoTime()
System.nanoTime() 独立于系统时钟。

当我收到当前服务器时间时,我会从系统中保存额外的 ns。然后根据服务器时间接收到的ns时间与当前nanoTime加上服务器时间之差计算出当前服务器时间。

示例:

// The Client starts and receive the current Server time and the nanoTime
private long         serverTime = server.getCurrentTime();
private long         systemNano = System.nanoTime();

//To calculate the current Server time without a service call
                                                                        //ns-> ms
long currentServerTime = serverTime + ((System.nanoTime() - systemNano) / 1000000);

谢谢

I find a Solution that fits perfectly for my situation.
Instead of using System.currentTimeMillis(), I'm using System.nanoTime().
System.nanoTime() is independent from the System clock.

When I receive the current Server time, I save additional the ns from the System. Then the current Server time will be calculated with the difference between the ns time from Server time receiving and the current nanoTime plus the Server time.

Example:

// The Client starts and receive the current Server time and the nanoTime
private long         serverTime = server.getCurrentTime();
private long         systemNano = System.nanoTime();

//To calculate the current Server time without a service call
                                                                        //ns-> ms
long currentServerTime = serverTime + ((System.nanoTime() - systemNano) / 1000000);

Thx

久隐师 2025-01-10 21:42:37

执行此操作的一种方法是获取服务器时间和本地时间之间的差异,并将其用于时间计算

  • 客户端启动并从服务器获取时间(我假设使用以毫秒为单位的当前时间,但这可以是适应您正在使用的任何内容)
  • 客户端检查其当前系统时间并保存差异
  • 然后可以始终将差异应用于客户端上的当前系统时间以计算服务器时间

示例:

long serverTime = 1328860926471l; // 2012/02/10 10:02, received from wherever
long currentTime = System.currentTimeMillis(); // current client time
long difference = currentTime - serverTime;

// Server time can then me retrieved like this:
long currentServerTime = System.currentTimeMillis() - difference;     
Date serverTimeDate = new Date(currentServerTime);

显然必须在服务器启动时保存差异时间已收到。

One way to do this is to get the difference between the server time and the local time and use that for time calculations

  • The client starts and gets the time from the server (I am going to assume use the current time in milliseconds but this can be adapted to whatever you are using)
  • The client checks its current system time and saves the difference
  • The difference can then always be applied to the current system time on the client to calculate the server time

Example:

long serverTime = 1328860926471l; // 2012/02/10 10:02, received from wherever
long currentTime = System.currentTimeMillis(); // current client time
long difference = currentTime - serverTime;

// Server time can then me retrieved like this:
long currentServerTime = System.currentTimeMillis() - difference;     
Date serverTimeDate = new Date(currentServerTime);

Obviously the difference must be saved the moment the server time is received.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文