This article introduces a simple network program roundtrip, which is used to measure the network latency between two machines, that is, "roundtrip time/round trip Time/rtt". This article mainly examines the tcp_nodelay of the fixed-length TCP message and the function of the packet.
The code for this article is shown in http://code.google.com/p/muduo/source/browse/trunk/examples/roundtrip/roundtrip.cc
The method of measuring RTT is simple:
Host A sends a message to host B that contains the local time that host a sends a message
Once host B receives the message echo back to host A
When host A receives a message, the RTT is obtained by subtracting the time in the message with the current time.
The NTP protocol works like this, but in addition to measuring the RTT,NTP need to know the time difference between the two machines (clock offset), so that the timing can be calibrated.
Above is the NTP protocol to send and receive messages, RTT = (t4-t1) – (T3-T2), Time difference = ((T4+T1)-(T2+T3))/2. NTP requires a one-way delay on the round-trip path to be as equal as possible to reduce system error. The accidental error is determined by the uncertainty of one-way delay.
In the roundtrip sample program I designed, the protocol simplifies: