将应用程序时间与外部服务器时间同步的最佳方法是什么? [英] What would be the best way to synchronize my application's time with outside server's time?

查看:89
本文介绍了将应用程序时间与外部服务器时间同步的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在考虑将系统的本地时间更改为服务器的时间,然后再使用它,但是我敢肯定还有其他方法可以做到这一点。我一直在尝试在C#中找到类似时钟的内容,但找不到任何内容。我正在以DateTime格式接收服务器的时间。

I was thinking of changing system's local time to server's time and then use it but I bet there are other ways to do this. I've been trying to find something like a clock in c#, but couldnt find anything. I'm receiving server's time in a DateTime format.

编辑:
我需要在与服务器相同的时间使用我的应用程序。我只想获取服务器的时间,然后再使用从服务器获取的时间使应用程序在while循环中工作。我的系统时间和服务器时间(甚至5秒)之间可能存在差异,这就是为什么我要这样做。

edit: I need my application to use while working same time server does. I just want to get server's time once and after that, make my application work in a while loop using the time I've obtained from the server. There might be a difference between my system's time and server's time (even 5 seconds) and that's why I want to do this.

推荐答案

@JonSkeet提供的同步时间的答案看起来不错,我只想指出一些事情。

The answer @JonSkeet's provided to synch the times looks good, I just wanted to point out some things.

正如@Alexei已经说过的那样,用户需要管理员权限才能更改其本地时间(至少在Windows中是这样),但可能还会导致其他问题时间不同步(互联网连接不良,黑客入侵等)。这意味着不能保证客户端时间确实与服务器时间相同,因此无论如何您至少需要检查在服务器端收到请求的时间。另外,这里可能还会有可用性问题,我是否希望应用程序能够更改自己本地计算机的时间?地狱号。

As @Alexei already said, users require admin privileges to be able to change their local time (in Windows as least), but there may also be other issues that can cause the time to be out of synch (bad internet connection, hacks etc.). This means there is no guarantee that the client time is indeed the same as the server time, so you will at least need to check the time the request was received serverside anyway. Plus there might also be a usability issue at hand here, would I want an application to be able change the time of my own local machine? Hell no.

总结一下:


  • 检查时间至少请求服务器端

  • 不更改客户端计算机的时间,而是在应用程序中显示某种指示符

如何处理应用程序中的指示符可以通过多种方式完成。

How to handle the indicator in your application can be done in various ways.


  • 在应用程序中显示一个与服务器定期同步的时钟(您的最初想法)

  • 显示某种倒计时(您可以在x秒后提交.. ),在收到请求后将resetCountdown请求推送给客户端。

  • 启用发送按钮或您拥有的所有按钮,其效果类似于倒数计时。

  • Show a clock in your application (your initial idea) that is periodically synched with the server
  • Show some kind of countdown ("you can submit after x seconds.."), push a resetCountdown request to the clients when a request is received.
  • Enable a 'send button' or what ever you have, this would work kind of similar to the countdown.

请记住,差不多无法验证该客户端等请求。因此,您必须在服务器端建立一些检查!

Just remember, it's nearly impossible validate a request such as this clientside. So you have to build in some checks serverside!

我实际上想写评论,但是它有点长..:)

I actually wanted to write a comment but it got kind of long.. :)

这篇关于将应用程序时间与外部服务器时间同步的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆