简体   繁体   English

在C中的客户端和服务器程序之间同步时钟

[英]Synchronizing clock between client and server programs in c

I am working on a project where a server parses audio files, and then tells clients to play certain sections of audio based on a common clock, the server's. 我正在一个项目中,服务器解析音频文件,然后告诉客户端根据服务器的通用时钟播放音频的某些部分。 In theory we would need a high level of synchronization as the client nodes might be playing audio right beside each other, and all but the smallest of delays may produce a sound offset noticeable by the human ear. 从理论上讲,我们需要高度的同步,因为客户端节点可能正在彼此并排播放音频,并且除了最小的延迟之外,所有其他延迟都可能产生人耳可察觉的声音偏移。 I have been trying to find c code examples of how to address this problem and have had little luck. 我一直在尝试找到如何解决此问题的C代码示例,但运气不好。

I have read many of the Wikipedia articles about clock synchronization. 我已经阅读了许多有关时钟同步的Wikipedia文章。

http://en.wikipedia.org/wiki/Clock_synchronization http://en.wikipedia.org/wiki/时钟同步

The above articles lead me to believe PTP would be ideal for our implementation on a local network. 以上文章使我相信PTP对于我们在本地网络上的实现将是理想的。 But ideally we would like to expand our project to allow for connection over the internet. 但是理想情况下,我们希望扩展我们的项目以允许通过Internet进行连接。 This lead me to believe NTP would be better. 这使我相信NTP会更好。 I visited NTP.org but found it difficult to understand, and it seemed like if my applications are on a local network, which isn't connected to the internet, I couldn't use NTP at all. 我访问了NTP.org,但发现它很难理解,而且好像我的应用程序位于未连接到Internet的本地网络上,则根本无法使用NTP。 But then I found this post: 但是后来我发现了这篇文章:

Small footprint clock synchronization without NTP 无需NTP的小型时钟同步

And Seth Robertson's response lead me to believe I was mistaken. 塞思·罗伯逊(Seth Robertson)的回答使我相信我错了。

you can get synchronized to a chosen system's clock using a few configuration file lines (four lines on each client, five lines on the server). 您可以使用一些配置文件行(每个客户端上四行,服务器上五行)与选定的系统时钟同步。

The problem is, he didn't show those lines of code, or point me to a place where I could view code that handles these problems. 问题是,他没有显示这些代码行,也没有指向我指出可以查看处理这些问题的代码的地方。

I've been looking around stackoverflow, and found many similar questions, but none that gave satisfactory answers. 我一直在研究stackoverflow,发现了许多类似的问题,但是没有一个给出令人满意的答案。

So, after a wall of text (sorry about that, the stack overflow question advice said to be very specific and explain the research I had done before asking a question), I would like to succinctly state my question, and hopefully someone can help me out: 因此,经过一连串的文字讨论(很抱歉,堆栈溢出问题建议非常具体,并解释了我在提出问题之前所做的研究),我想简单地陈述一下我的问题,希望有人可以帮助我出:

In a client/server software model, how can I maintain an accurate clock synchronization within the software to allow event execution based on time? 在客户端/服务器软件模型中,如何在软件内保持准确的时钟同步,以允许基于时间执行事件?

Currently our group is using UDP, but we are willing to change if necessary. 目前,我们的小组正在使用UDP,但是如果有必要,我们愿意进行更改。

Thanks for any help. 谢谢你的帮助。

EDIT: I understand that I need to maintain a clock sync, the question is, HOW do I do this in software. 编辑:我知道我需要保持时钟同步,问题是,我该如何在软件中执行此操作。 I'm looking for example code, or a hyperlink to a place where I can find sample code on this subject. 我正在寻找示例代码,或者是指向可以在该主题上找到示例代码的地方的超链接。 ENDEDIT 结束编辑

The concept is clear: Select one host in your network to be the NTP server, and let the other hosts synchronise themselfs with this server. 概念很明确:在网络中选择一台主机作为NTP服务器,然后让其他主机将自己与该服务器同步。

If your need is just synchronisation between the hosts of your LAN this should do. 如果您仅需要LAN主机之间的同步,就应该这样做。

NTP servers as well as clients are freely available for a lot of systems. NTP服务器和客户端可免费用于许多系统。

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

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