Synchronizing time between two PCs via TCP/IP, no

2019-07-09 20:02发布

问题:

I've got two PCs connected by LAN. When the user alters the time (via my application) on one, I'd like the time on the second to be updated too (or the second machine could poll the first). There's no server, no internet. What's the best way to do this?

OS: XP embedded.
Framework: .Net 3.5

回答1:

If you can't use NTP server you have to write it by yourself.

  1. The two application both implements a WCF service and are one client of the other;
  2. The application that have its local time modified notifies the other and sends a DateTime;
  3. The application that receives the updated DateTime changes local time (but be aware that your application will need higher privilege to do this because you have to do it via unmanaged code);

If you don't need much precision you are done. If you need greater precision, you will need to estimate the time lost between the data sending and arrivals and is not trivial.



回答2:

A low tech solution might be to execute the command NET TIME \\[OtherPC] /SET on the machine that needs to be updated.

You could possibly set the machine up to call this at a certain interval.

Please note, I've only tried it on normal windows machines, but I think it would also work on embedded.

Edit: Looking around it seems like XP Embedded might support WMI, if it does and it does have the Win32_OperatingSystem class and it's SetDateTime method, your app could make that call to the remote pc and you could avoid having to poll for changes.



回答3:

Is it possible to designate one of the machines as an NTP server? and hence use a standard NTP server system so you don't have to write your own.

Although I am not familiar with XPe this link Got the time? seems to have all the required buzzwords