测量联网设备之间的时间差 [英] Measuring time difference between networked devices

查看:69
本文介绍了测量联网设备之间的时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将联网多人游戏添加到我制作的游戏中.当服务器向客户端发送更新数据包时,我会包含一个时间戳,以便客户端确切知道该信息何时有效.但是,服务器计算机和客户端计算机的时钟可能设置为不同的时间(甚至可能只有几秒钟的差异),因此需要将来自服务器的时间戳转换为客户端的本地时间.

I'm adding networked multiplayer to a game I've made. When the server sends an update packet to the client, I include a timestamp so that the client knows exactly when that information is valid. However, the server computer and the client computer might have their clocks set to different times (maybe even just a few seconds difference), so the timestamp from the server needs to be translated to the client's local time.

所以,我想知道计算服务器和客户端之间的时间差的最佳方法.目前,客户端在初始化期间 ping 服务器以获取时间戳,记录请求的发送时间和响应时间,并猜测时间戳大约是在旅程的中途生成的.客户还运行了 10 次这样的试验并取平均值.

So, I'd like to know the best way to calculate the time difference between the server and the client. Currently, the client pings the server for a time stamp during initialization, takes note of when the request was sent and when it was answered, and guesses that the time stamp was generated roughly halfway along the journey. The client also runs 10 of these trials and takes the average.

但是,问题是我在重复运行程序时得到了不同的结果.在每组 10 个中,每次测量的偏差很少超过 400 毫秒,这可能是可以接受的.但是,如果我在每次程序运行之间等待几分钟,结果平均值可能会相差 2 秒之多,这是不可接受的.

But, the problem is that I'm getting different results over repeated runs of the program. Within each set of 10, each measurement rarely diverges by more than 400 milliseconds, which might be acceptable. But if I wait a few minutes between each run of the program, the resulting averages might disagree by as much as 2 seconds, which is not acceptable.

有没有更好的方法来找出两个联网设备的时钟之间的差异?或者至少有一种方法可以调整我的算法以产生更准确的结果?

Is there a better way to figure out the difference between the clocks of two networked devices? Or is there at least a way to tweak my algorithm to yield more accurate results?

可能相关也可能不相关的详细信息:这些设备是通过蓝牙进行通信的 iPod Touch.我正在测量 ping 的时间为 50-200 毫秒.我不能要求用户同步他们的时钟.:)

Details that may or may not be relevant: The devices are iPod Touches communicating over Bluetooth. I'm measuring pings to be anywhere from 50-200 milliseconds. I can't ask the users to sync up their clocks. :)

更新:在以下答案的帮助下,我编写了一个 Objective-c 类来处理这个问题.我把它贴在我的博客上:http://scooops.blogspot.com/2010/09/timesync-was-time-sink.html

Update: With the help of the below answers, I wrote an objective-c class to handle this. I posted it on my blog: http://scooops.blogspot.com/2010/09/timesync-was-time-sink.html

推荐答案

我最近上了一小时的课,时间不够长,但我会尽量把它归结起来让你指出正确的方向.准备好学习一点代数.

I recently took a one-hour class on this and it wasn't long enough, but I'll try to boil it down to get you pointed in the right direction. Get ready for a little algebra.

让我们根据服务器的时间相等.根据客户,让 c 等于时间.让 d = s - c.d 是添加到客户端的时间以将其纠正为服务器的时间,这也是我们需要解决的问题.

Let s equal the time according to the server. Let c equal the time according to the client. Let d = s - c. d is what is added to the client's time to correct it to the server's time, and is what we need to solve for.

首先我们从服务器向客户端发送一个带有时间戳的数据包.当客户端收到该数据包时,它将给定时间戳与其自身时钟之间的差异存储为 t1.

First we send a packet from the server to the client with a timestamp. When that packet is received at the client, it stores the difference between the given timestamp and its own clock as t1.

然后客户端将带有自己的时间戳的数据包发送到服务器.服务器将时间戳与其自身时钟之间的差值作为 t2 发送回客户端.

The client then sends a packet to the server with its own timestamp. The server sends the difference between the timestamp and its own clock back to the client as t2.

请注意,t1 和 t2 都包括数据包的旅行时间"t 加上两个时钟之间的时间差 d.暂时假设两个方向的旅行时间相同,我们现在有两个未知数的两个方程,可以求解:

Note that t1 and t2 both include the "travel time" t of the packet plus the time difference between the two clocks d. Assuming for the moment that the travel time is the same in both directions, we now have two equations in two unknowns, which can be solved:

t1 = t - d
t2 = t + d
t1 + d = t2 - d
d = (t2 - t1)/2

诀窍在于旅行时间并不总是恒定的,正如您在 50 到 200 毫秒之间的 ping 所证明的那样.事实证明,使用具有最短 ping 时间的时间戳是最准确的.那是因为您的 ping 时间是裸机"延迟加上在路由器队列中等待的任何延迟的总和.每隔一段时间,幸运数据包就会通过而不会出现任何排队延迟,因此您可以将该最短时间用作最可重复的时间.

The trick comes because the travel time is not always constant, as evidenced by your pings between 50 and 200 ms. It turns out to be most accurate to use the timestamps with the minimum ping time. That's because your ping time is the sum of the "bare metal" delay plus any delays spent waiting in router queues. Every once in a while, a lucky packet gets through without any queuing delays, so you use that minimum time as the most repeatable time.

还要记住,时钟以不同的速率运行.例如,我可以将家里的计算机重置为毫秒,一天后它会慢 8 秒.这意味着您必须不断重新调整 d.您可以使用随时间计算的 d 的各种值的斜率来计算您的漂移并在两次测量之间对其进行补偿,但这超出了此处答案的范围.

Also keep in mind that clocks run at different rates. For example, I can reset my computer at home to the millisecond and a day later it will be 8 seconds slow. That means you have to continually readjust d. You can use the slope of various values of d computed over time to calculate your drift and compensate for it in between measurements, but that's beyond the scope of an answer here.

希望能帮助您指明正确的方向.

Hope that helps point you in the right direction.

这篇关于测量联网设备之间的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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