Protocol Package:
The main fields are interpreted as follows:
L LI (Leap indicator): The length is 2 bits, the value is "11" when the alarm state, the clock is not synchronized. NTP itself does not process for other values.
L VN (version number): 3 bits in length, the version of NTP, the current version is 3.
L Mode: Length is 3 bits, indicating the mode of operation of NTP. Different values represent the following meanings: 0 undefined, 1 for active peer mode, 2 for passive peer mode, 3 for customer mode, 4 for server mode, 5 for broadcast mode or multicast mode, 6 for NTP control message, 7 for internal use.
L Stratum: The number of layers of the system clock, the value range is 1~16, it defines the accuracy of the clock. The number of clocks with a layer of 1 is the highest, the accuracy is decreased from 1 to 16, and the clock with a layer of 16 is in an unsynchronized state and cannot be used as a reference clock.
L Poll: Polling time, that is, the time interval between two consecutive NTP messages.
L Precision: The accuracy of the system clock.
L Root Delay: Roundtrip time from the local to the main reference clock source.
L Root Dispersion: The maximum error of the system clock relative to the main reference clock.
L Reference Identifier: Identification of reference clock source.
L Reference Timestamp: The time when the system clock was last set or updated.
L originate the local time of the sending port when the TIMESTAMP:NTP request message leaves the sender.
L Receive TIMESTAMP:NTP The local time of the receiving end when the request message arrives at the receiving end.
L Transmit Timestamp: The local time of the respondent when the answer message leaves the respondent.
L Authenticator: Verify information.
Class NetworkUInt64 {Public:operator UINT64 () {return HTONLL (ndata);
} const networkuint64& operator = (UINT64 nvalue) {ndata = HTONLL (Nvalue);
return *this;
} Protected:uint64 Ndata;
};
Const static ULONGLONG n1970_1900_seconds = 2208988800;
__declspec (align (1)) struct Ntpdata {unsigned int mode:3;
unsigned int versionnumber:3;
unsigned int leapindicator:2;
unsigned int stratum:8;
unsigned int poll:8;
unsigned int precision:8;
unsigned int rootdelay:32;
unsigned int rootdispersion:32;
unsigned int referenceidentifier:32;
NetworkUInt64 Referencetimestamp;
NetworkUInt64 Originatetimestamp;
NetworkUInt64 Receivetimestamp;
NetworkUInt64 Transmittimestamp;
}; int _tmain (int argc, _tchar* argv[]) {setlocale (Lc_all, ".
ACP ");
Wsadata Wsadata;
SOCKET Sendsocket;
Sockaddr_in recvaddr;
int Port = 123;
---------------------------------------------//Initialize Winsock WSAStartup (Makeword (2, 2), &wsadata); //---------------------------------------------//Create a socket for sending data Sendsocket = socket (af_inet, SOCK_DGRAM, Ipproto_u
DP); ---------------------------------------------//Set up the RECVADDR structure with the IP address of//the receiver
(In this example case "123.456.789.1")//and the specified port number.
recvaddr.sin_family = af_inet;
Recvaddr.sin_port = htons (port);
hostent* phostent = gethostbyname ("time.nist.gov");
if (phostent!= NULL) {RecvAddr.sin_addr.s_addr = * (U_long *) phostent->h_addr_list[0];; }//---------------------------------------------//Send a datagram to the receiver printf ("Sending a datagram to the"
Receiver...\n ");
Set version number to 3 and Mode to 3 (client) Ntpdata data = {0}; Data.
versionnumber = 3; Data.
Mode = 3;
int tv_out = 10000;
SetSockOpt (Sendsocket, Sol_socket, So_rcvtimeo, (char*) &tv_out, sizeof (tv_out));
__time64_t starttime = _time64 (NULL); SendTo (Sendsocket, (char*) &data, sizeof (data), 0, (SOCKADDR *) &recvaddr, sizeof (RECVADDR));
Sockaddr fromaddr = {0};
int nread = sizeof (FROMADDR); if (Socket_error!= recvfrom (Sendsocket, (char*) &data, sizeof (data), 0, &fromaddr, &nread)) {__time64_t CU
Rtime = _time64 (NULL); __time64_t Serverreceivetime = (UINT64 (data.
Receivetimestamp) >>)-n1970_1900_seconds; __time64_t Servertransmittime = (UINT64 (data.
Transmittimestamp) >>)-n1970_1900_seconds;
else {cout << "WSAGetLastError:" << wsagetlasterror () << Endl;
}//---------------------------------------------//When the application are finished sending, close the socket. printf ("finished sending.
Closing socket.\n ");
Closesocket (Sendsocket);
---------------------------------------------//clean up and quit.
printf ("exiting.\n");
WSACleanup ();
System ("pause");
return 0; }
field interpretation from http://blog.163.com/yzc_5001/blog/static/2061963420121283050787/