简体   繁体   English

同步客户端/服务器Java套接字之间的时间

[英]Synchronize time between client/server java sockets

Here, I am developing a project with java sockets and I have a problem with the implementation of the correct time synchronization between Server and Client. 在这里,我正在使用java套接字开发一个项目,我在服务器和客户端之间实现正确的时间同步时遇到了问题。 I am going to describe the problem with a simple example: 我将用一个简单的例子来描述问题:

Server runs on GMT and keeps a database with various items. 服务器在GMT上运行,并使用各种项目保存数据库。 Some of those items are on special offer, but these offers have a time limit before they expire. 其中一些物品是特价商品,但这些商品在到期前有时间限制。
So let's say that server time now is 9:00AM(GMT) and there is an offer item ending at 10:00AM(GMT) . 因此,假设服务器时间现在是上午9:00(格林威治标准时间),并且有一个优惠项目在上午10:00(格林威治标准时间)结束。

Client may be on a different time and timezone than the server. 客户端可能与服务器位于不同的时间和时区。 So let's say that client time now is 8:00AM(GMT-1) , I can take the time and adjust it to client's timezone and find that it ends at 9:00AM(GMT-1) ie in 1 hour. 因此,假设现在客户端时间是上午8:00(GMT-1) ,我可以花时间将其调整到客户的时区,并发现它在上午9:00(GMT-1)结束,即在1小时内结束。

Problem: How do I calculate the time remaining when a user has a custom time set. 问题:如何计算用户设置自定义时间的剩余时间。
For example, the above client sets the clock manually to be half an hour ahead, ie 8:30AM(GMT-1) . 例如,上述客户端手动将时钟设置为提前半小时,即上午8:30(GMT-1) If you just do the timezone conversion the item would still be ending at 9:00(GMT-1) , so the time left for the offer to finish is wrong (30mins). 如果您只是进行时区转换,该项目仍将在9:00(GMT-1)结束 ,因此要约完成的时间是错误的(30分钟)。

One may say that a possible solution would be to set the client to ask the time left in seconds from the server instead of the exact date ending. 可以说可能的解决方案是将客户端设置为从服务器询问剩余的时间,而不是确切的结束日期。 But I want to implement something like a count down of seconds in the client side. 但是我希望在客户端实现类似秒数的计数。 (If offer ends in 60 seconds, the interface would go 60,59,58,..,1,0). (如果报价在60秒内结束,界面将为60,59,58,...,1,0)。 So sending a request every second to the server to get the time left is not be network efficient. 因此, 每秒向服务器发送一个请求以获得剩余时间不是网络效率。

One more thing that worries me is that if you do go with the scenario of requesting the "time left in seconds", on slow network the response from server will not come instantly, so by the time the client receives the result is already off time. 还有一件令我担心的事情是,如果你确实采用了请求“以秒为单位的时间”的情况,在慢速网络上服务器的响应将不会立即发生,所以当客户端收到结果时已经是关闭时间。

The simpliest way is not to use client time at all, as it is unreliable. 最简单的方法是不使用客户端时间,因为它不可靠。

Just ask the server time periodically. 只需定期询问服务器时间。 You don't have to do each and every second, but rather before anything important happens (order is made) 您不必每时每刻都做,而是在发生任何重要事件之前(订单已做)

you can use NTP protocol. 你可以使用NTP协议。 to sync between Server/Client. 在服务器/客户端之间同步。

NTP standard uses GMT as time reference, and depend on you location you choose the closest ntp pool. NTP标准使用GMT作为时间参考,并根据您选择最近的ntp池的位置。

depends on where you are in the world NIST (in us) implements NTP 取决于你在世界的哪个位置NIST (在我们中)实现了NTP

NTP practically uses stratum server with time source a "cesium clock", with error fraction of nanosecond. NTP实际上使用时间源为“铯钟”的层服务器,误差分数为纳秒。 used in GPS, money transaction, and your computer probably (if it's unix-like) 用于GPS,货币交易和您的计算机(如果它是unix一样的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM