Time synchronization, use NTP or just socket

ntppingsockettime

I am using a PC and a raspberry pi(both run Linux) to gather sensor data, raspberry pi gathers laser scan data, PC gathers image captured by camera. I must synchronize time for these two machines. I hope the time difference between them can be less than 1ms.

I connect them directly with a network capble, so I can't use some well known NTP servers. I pinged PC on raspberry pi, the ping time is less than 0.6ms. I come up with two ideas:

  1. Regard the PC as a NTP server, raspberry pi synchronize time from it.
  2. Create a socket connection between them, raspberry pi just send its timestamp to PC when the connection is established, thus we can know the time difference of two machines.

Which one is a better idea? Or do you have any better ideas? How to calculate the actual time difference between two machines? Can I limit the time difference to 1ms?

Best Answer

The socket idea is the old venerable time protocol documented in RFC 868. The utility that synchronizes system time based on that protocol is called rdate.

You're better off using NTP because NTP will track how the clocks of the two systems naturally drift apart over time and correct for it. The time protocol should be reserved for situations where NTP is infeasible, such as embedded systems that are too tight on resources to run something as complicated as NTP (which the rpi is not).

Related Question