title: Time synchronization over the network in C++
body: <p>I'm modding a game (Portal 2). I want to add a countdown function to start a race.</p>
  
  <p>Using SFML library, I'm sending <code>time_since_epoch</code> of a <code>system_clock</code> to the local computer and a distant one through sockets. Once it's received, I compare the time_since_epoch received and the one given by the <code>now()</code> function.</p>
  
  <p>So that I'm able to get the time that the socket took to send the message, which should be the current ping (~4ms).</p>
  
  <p>It works fine on the same computer executing the server. However, it gives me a very big number on a distant one.
  Here's my code:</p>
  
  <p><strong>Server side:</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>void NetworkManager::StartCountdown()
  {
      sf::Uint64 epoch = std::chrono::time_point_cast&lt;std::chrono::milliseconds&gt;(std::chrono::system_clock::now()).time_since_epoch().count();
  
      sf::Packet countdown_packet;
      countdown_packet &lt;&lt; HEADER::COUNTDOWN &lt;&lt; epoch;
      for (auto&amp; socket : this-&gt;socket_pool) {
          socket-&gt;send(countdown_packet);
      }
  }
  </code></pre>
  
  <p><strong>Client side:</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>void NetworkGhostPlayer::Countdown(sf::Uint64 epoch)
  {
      //std::this_thread::sleep_for(std::chrono::milliseconds(4));
      auto now = std::chrono::system_clock::now();
      auto diff = std::chrono::time_point_cast&lt;std::chrono::milliseconds&gt;(now).time_since_epoch().count() - epoch;
      console-&gt;Print("%lld\n", diff);
  }
  </code></pre>
  
  <p><code>Countdown()</code> is called after receiving the packet and informations in it. <code>console-&gt;Print()</code> is a Portal 2 function to print in the game console.
  Portal 2 is executing the client side.</p>
  
  <p>On the local computer, the console prints <code>0</code>. But on the distant one, it gives numbers between <code>300</code> and <code>1000</code>.
  I though it was related to the ping, which is of ~4ms, but I tested on the local computer with the <code>std::this_thread::sleep_for(std::chrono::milliseconds(4));</code> line, and the output was <code>4</code> as expected.</p>
  
  <p>I don't understand what is the problem. If I execute the server on another computer, then the new local game instance gives 0 and the new distant one gives a big number.</p>
  
  <p>Both computers are on Windows. Here's the complete code of the plugin : <a href="https://github.com/Blenderiste09/SourceAutoRecord/blob/Network-ghosts/GhostServer/NetworkManager.cpp" rel="nofollow noreferrer">https://github.com/Blenderiste09/SourceAutoRecord/blob/Network-ghosts/GhostServer/NetworkManager.cpp</a></p>
  
  <p>and </p>
  
  <p><a href="https://github.com/Blenderiste09/SourceAutoRecord/blob/Network-ghosts/src/Features/Demo/NetworkGhostPlayer.cpp" rel="nofollow noreferrer">https://github.com/Blenderiste09/SourceAutoRecord/blob/Network-ghosts/src/Features/Demo/NetworkGhostPlayer.cpp</a></p>
  
  <p>Thank you very much for your help !</p>
  
  <p>Blenderiste09</p>