Time format: Monday, July 26th, 2004 at 09: 58: 57.
Time.nist.gov is the American Institute of Standards and Technology, which uses Greenwich Mean Time.
Time format: 5321204-07-2602: 00:125000488.3 utc (NIST) *
Privately declare the sub-sleep library "kernel32" (as long as byval dw mills).
Dim NoSrv is a boolean value.
Dim TimeFromNet
Private Sub-Form _Load ()
Winsock 1。 "protocol = scktcpprotocol" adopts TCP protocol.
NetTime "www.time.ac.cn "'first takes the time of National Time Service Center of Chinese Academy of Sciences.
If NoSrv or TimeFromNet = ""then
If there is no time in the National Time Service Center of Chinese Academy of Sciences, the time in the American Institute of Standards and Technology will be taken.
NetTime "time.nist.gov "
If NoSrv or TimeFromNet = ""then
If the time of American Institute of Standards and Technology cannot be used, an error will be reported.
MsgBox "time.nist.gov, the network standard time server, cannot be detected!"
other
In order to reduce the network transmission error, the time of the American Institute of Standards and Technology was adopted for the second time.
NetTime "time.nist.gov "
If TimeFromNet = ""then
Msgbox "time.nist.gov, network standard time server, timeout!"
other
TimeFromNet = Mid(TimeFromNet, 17,8)
TimeFromNet = TimeSerial ((TimeFromNet)+8) Mod 24, TimeFromNet, TimeFromNet))
Time = TimeFromNet' Set the system time.
If ... it will be over.
If ... it will be over.
other
In order to reduce the error of network transmission, the time of National Time Service Center of Chinese Academy of Sciences was calibrated twice.
NetTime "www.time.ac.cn "
If TimeFromNet = ""then
Msgbox "www.time.ac.cn, network standard time server, timeout!"
other
Time = mid (timefromnet,12,8)' sets the system time.
If ... it will be over.
If ... it will be over.
end
End joint
Close Winsock subroutine.
Private Sub Winsock 1_Close ()
If Winsock 1. Status & lt& gt was closed at that time.
Winsock 1。 close
If ... it will be over.
End joint
Winsock receives data events.
Private sub Winsock1_ data arrival (byval bytes total as long)
TimeFromNet = String(bytesTotal," ")
Winsock 1。 GetData TimeFromNet,vbString,bytesTotal
End joint
Winsock error event
Private sub Winsock1_ error (byval number is an integer, Description is a string, ByVal Scode is a Long, ByVal Source is a string, ByVal HelpFile is a string, ByVal HelpContext is a Long, and CancelDisplay is a Boolean).
NoSrv = True
End joint
Get the standard time from a website that provides the standard time on the Internet.
Private Sub NetTime(TimeSrv as a string)
NoSrv = False
TimeFromNet = " "
If Winsock 1. Status & lt > sckclosedthen Winsock1. close
Winsock1.remotehost = timesrv "www.time.ac.cn" or "time.nist.gov".
Winsock 1。 Remote port = 13
Winsock 1。 Local port = 0
Winsock 1。 connect
Do While TimeFromNet = ""'Wait for the standard time website to return time data.
If NoSrv exits Do, if Winsock makes an error, jump out of the loop and wait.
Sleep 55
Multiple activities
ring
If Winsock 1. Status & lt > sckclosedthen Winsock1. close
End joint
Search for more posts on related topics: Internet standards.
The RFC-867 standard and VB routine are introduced above. Obviously, the format used by RFC-867 to return the current time and date is a string format and there is no specific format for daytime (for example, the National Time Service Center of Chinese Academy of Sciences is "Mon Jul 26 09:58:57 2004", while the American Institute of Standards and Technology is "5321204-07-2602: 00:/kloc-0".
RFC868 time protocol
(RFC868 time protocol)
This RFC specifies the standards of ARPA Internet community. All hosts on ARPA Internet should adopt and implement this standard.
This protocol provides site-independent and machine-readable date and time information. The time service is returned in seconds, that is, 1900 65438+ 10 month 1 the number of seconds from midnight to now. God, it's not small.
An important purpose of designing this protocol is that many hosts on the network have no concept of time. On the distributed system, we can consider how to divide the time between Beijing and Tokyo. The time of the host computer can often be changed artificially because of the error of the machine clock. Therefore, it is necessary to use the time server to obtain network time through election, so that the server has an accurate concept of time. Don't underestimate time, for some simple time-based distributed programs, time is too important.
This protocol can work under TCP and UDP protocols. The following is the workflow of time protocol working through TCP protocol: here s stands for server and u stands for client.
Detection port 37
U: connected to port 37
S: sending time, 32-bit binary number.
U: receiving time
U: close the connection
Close the connection
The server listens for connections on port 37. When the connection is established, the server returns a 32-bit time value and then closes the connection. This process is not difficult. If the server can't decide what time it is, the server will refuse to connect or close the connection directly without sending any data.
Let's look at the usage of UDP protocol: here s stands for server and u stands for client.
Detection port 37
U: Send a null data report to port 37.
Receive this empty datagram.
S: Send a datagram containing a 32-bit binary number (used to indicate time).
U: receiving time datagram
The server listens for packets on port 37. When a data packet arrives, the server returns a data packet containing 32 bits of time. This process is not difficult. If the server can't decide what time it is, it will discard the received datagram without any reply.
* Time
The time is expressed in 32 bits, which is the number of seconds starting from 0: 00 in June 1900 65438+ 10/0. We can calculate that this agreement can only be used until 2036. But we also know that computers are developing so fast that there may be a better protocol to replace this protocol, or an effective solution has been worked out. )
Here are some examples:
The time of 2,208,988,800 corresponds to 00:00 1 1 month 1970 GMT.
2,398,291,200 corresponds to 00:00 1 Jan 1976 GMT,
2,524,521,600 corresponds to 00:00 1 Jan 1980 GMT,
2,629,584,000 corresponds to 00: 001may 1983 GMT,
And letters from-1, 297,728,000 to 00: 00171month 1858 GMT.
Next, we use VB program to set our own computer system time through RFC868 protocol. In order to simplify the program, the program does not correct the date, but only the time. But this routine is much more perfect than the above program. First, it can read the time data of 20 standard time servers around the world. Second, it uses the compensation of network delay. Third, filter the standard time servers whose network delay exceeds 3 seconds. Add the 1 Winsock control to FORM 1, and cut and paste the following code into the code form of FORM 1:
Option explicit
The time protocol (RFC-868) provides a 32-bit number to represent the seconds between 1900 1+0.
Time is UTC (representing world time coordinates regardless of alphabetical order),
It is similar to the so-called GreenwichMeanTime or GMT-Greenwich Mean Time.
A program that uses TCP to obtain accurate time should have the following steps:
1 Connect to port 37 that provides this service;
Receive 32 bits twice;
3 Close the link.
Privately declare the sub-sleep library "kernel32" (as long as byval dw mills).
Dim NoSrv is a boolean value.
Dim TimeFromNet stores the number of seconds read from the time website.
Dimtime URL (19) is used as the string' 20 times to provide the URL of the website.
Program entrance
Private Sub-Form _Load ()
I'm as long as single.
Dimhh is an integer, mm is an integer and ss is an integer' hours, minutes and seconds.
Me. show
CDec (TimeFromNet)' converted to decimal subtype, 28-bit integer.
TimeURL(0) = "www.time.ac.cn "'First, take the time of National Time Service Center of Chinese Academy of Sciences.
Timeurl (1) = "time.nist.gov"' American Institute of Standards and Technology.
TimeURL(2)= " time-a . time freq . blrddoc . gov "
time URL(3)= " NIST 1 . datum . com "
TimeURL(4)= " NIST 1-DC . glassey . com "
TimeURL(5)= " NIST 1-ny . glassey . com "
TimeURL(6)= " NIST 1-SJ . glassey . com "
TimeURL(7)= " utc NIST . Colorado . edu "
TimeURL(8)= " time-b . time freq . blrddoc . gov "
TimeURL(9)= " time-c . time freq . blrddoc . gov "
TimeURL( 10)= " time-a . NIST . gov "
time URL( 1 1)= " time-b . NIST . gov "
TimeURL( 12)= " NIST 1 . AOL-va . truetime . com "
TimeURL( 13)= " NIST 1 . AOL-ca . truetime . com "
TimeURL( 14)= " time-NW . NIST . gov "
TimeURL( 15)= " Time-b . Time freq . blrdoc . gov "
TimeURL( 16)= " Time-c . Time freq . blrdoc . gov "
TimeURL( 17)= " ptbtime 1 . PTB . de "
TimeURL( 18)= " clock . CMC . EC . GC . ca "
TimeURL( 19)= " chronos . CSR . net "
For i = 0 to 19
Me. Caption = "Connecting-"&; Time Link (1)
The time when the service center was first read.
If (non-NoSrv) and TimeFromNet & gt If the time is read successfully, it is 0.
In order to reduce the network transmission error, the service center time is occupied again.
T0 = Timer' In order to reduce the error caused by network delay, first read the current time.
NetTime TimeURL(i)' Auxiliary Reading Time Service Center Time
If (non-NoSrv) and TimeFromNet & gt If the second reading is successful, it is 0.
Timefromnet = timefromnet+int ((timer-t0)/2+0.5)' plus network delay compensation (delay/2 is delay compensation).
Time from net = time from net-86400 * int (time from net/86400)' take the modulus in days (86400 seconds).
SS = TimeFromNet Mod 60' takes seconds.
TimeFromNet = TimeFromNet 60
MM = TimeFromNet Mod 60' score
HH = ((TimeFromNet 60) +8) Mod 24' hours (Beijing time +8)
"MsgBox" network delay: "&; (Timer-T0)
Time = TimeSerial(HH, MM, SS)' sets the system time.
Exit For' exits the loop after the timer ends.
If ... it will be over.
If ... it will be over.
Next, I
If I> then 19
MsgBox "Unable to get network time!"
If ... it will be over.
end
End joint
Close Winsock event.
Private Sub Winsock 1_Close ()
If Winsock 1. Status & lt& gt was closed at that time.
Winsock 1。 close
If ... it will be over.
End joint
Winsock receives data events.
Private sub Winsock1_ data arrival (byval bytes total as long)
Dim TmpData
Winsock 1。 GetData TmpData
time from net = TmpData(3)+TmpData(2)* 256+TmpData( 1)* 256 * 256+TmpData(0)* 256 * 256 * 256
End joint
Winsock error event
Private sub Winsock1_ error (byval number is an integer, Description is a string, ByVal Scode is a Long, ByVal Source is a string, ByVal HelpFile is a string, ByVal HelpContext is a Long, and CancelDisplay is a Boolean).
NoSrv = True
End joint
Get the standard time from a website that provides the standard time on the Internet.
Private Sub NetTime(TimeSrv as a string)
"Dim i As Integer" timeout counter
i = 0
NoSrv = False
TimeFromNet = 0
If Winsock 1. Status & lt > sckclosedthen Winsock1. close
Winsock 1。 "remotehost = timesrv" time provides the URL of the website.
Winsock 1。 "Remote Port = 37" Time Protocol (RFC-868) specifies the port.
Winsock 1。 Local port = 0
Winsock 1。 connect
Do While TimeFromNet & lt= 0
i = i + 1
If NoSrv or i> If Winsock makes an error or times out for about 3 seconds, time acquisition fails.
Sleep 55
Multiple activities
ring
If Winsock 1. Status & lt > sckclosedthen Winsock1. close
End joint
Editor: Marshal Hangzhou
The most accurate network time protocol should be RFC 1305-NTP (network time protocol), which can achieve the time accuracy of 1-50 ms, but the protocol is very complicated. I'm sorry I don't have the Chinese translation of RFC 1305 at hand. But later, a new RFC, RFC 1769-SNTP (Simple Network Time Protocol), was introduced, which simplified the operation and application scope required by RFC 1305. The following is an introduction to RFC 1769-SNTP:
Network working group D. Mills
Comments: 1769 University of Delaware
Outdated information:1361March 1995
Category: information
(RFC1769-Simple Network Time Protocol)
Status of this memorandum:
This memorandum provides information for the Internet community, but does not stipulate any type of Internet standards. There are no restrictions on the distribution of this memorandum.
summary
This memo describes Simple Network Time Protocol (SNTP), which is an adaptation of Network Time Protocol (NTP) and is suitable for synchronizing computer clocks on the Internet. When it is not necessary to realize all the functions of NTP described in RFC 1305, SNTP can be used. It can work in unicast mode (point to point) and broadcast mode (point to multipoint). It can also work in IP multicast mode (in this mode, this service can be provided). SNTP is not much different from the current and previous NTP versions. But it is simpler, a stateless remote procedure call (RPC), and its accuracy and reliability are similar to those expected by UDP/TIME protocol described in RFC868.
RFC 136 1 with the same title has been deleted from this memorandum. Its purpose is to explain the protocol mode of broadcast operation, provide further explanation in some places and correct some printing errors. The working mechanism described in NTP version 3 RFC 1305 is not necessary for the realization of SNTP. There are no restrictions on the distribution of this memorandum.
catalogue
1.
2. Working mode and address assignment
3.NTP timestamp format
4.NTP message format
5.SNTP customer operation
6.SNTP server operation
7. Reference materials
8. Security considerations
9. The author's address
1.
RFC 1305 [MIL92] specifies the Network Time Protocol (NTP) to synchronize computer clocks on the Internet. It provides comprehensive access to the national time and frequency propagation service mechanism, organizes time synchronization subnets and adjusts the time of each local clock in the participating subnets. In most areas of the Internet today, NTP provides an accuracy of1-50ms, which depends on the characteristics of synchronization sources and network paths.
RFC 1305 specifies the events, states, transmission functions and operations in NTP protocol mechanism. In addition, there are optional algorithms, which improve the quality of time measurement and reduce some possible errors in the synchronization source. It is necessary to use some complex algorithms or their equivalent algorithms to obtain the delay accuracy from the main path to the millisecond level on the Internet. However, in many cases, this kind of accuracy is unnecessary, and perhaps the accuracy of seconds is enough. In this case, a simpler protocol is used, such as "Time Protocol" [POS83]. These protocols are exchanged based on RPC: the client requests the current time, and then the server sends back the number of seconds from the known time to the present.
NTP is designed to be suitable for clients and servers with very different performances, and it is also suitable for situations where the network where the clients and servers are located has a wide range of network delay and jitter. Nowadays, most users of NTP synchronization subnet on the Internet use a software package, which contains a set of NTP selection and algorithms. This is a complex real-time application system. Software should be suitable for all kinds of hardware platforms: from supercomputers to personal computers. To be suitable for such a range, its huge size and complexity are not suitable for many applications. According to the requirements, explore some alternative access policies (using the ones that are suitable for the accuracy requirements)
Very strict simple software) useful.
This memo describes Simple Network Time Protocol (SNTP), which is a simplified NTP server and NTP client policy. The implementation of the SNTP Protocol has not changed and will not change in the near future. The access paradigm is consistent with UDP/TIME protocol. In fact, SNTP should be more suitable for using UDP/TIME client of personal computer. In addition, SNTP is designed to run on a special server (including an integrated radio clock). Due to the design and control of various reaction mechanisms in the system, it is possible to accurately adjust the time to microseconds. Such a special design is feasible.
It is strongly recommended to use SNTP only at the end of synchronous subnet. SNTP clients should only run in the leaves of the subnet (the highest level) and do not rely on other NTP or SNTP clients for synchronization during configuration. The SNTP server should only run on the root of the subnet (hierarchy 1), and there is no other synchronization source except the reliable radio clock during configuration. Only by using redundant synchronization sources, different subnet paths and elaborate algorithms in the whole NTP implementation can the reliability that the main server usually expects be achieved. This method enables the main synchronization source to use several other radio clocks and alternate paths to other main servers when the radio clock communication fails or the wrong time is passed. Therefore, we should carefully consider using SNTP on the client instead of NTP on the main server.
2. Working mode and address assignment
Like NTP, SNTP can operate in unicast (point-to-point) or broadcast (point-to-multipoint) mode. The unicast client sends a request to the server and expects a reply from it, and (optionally) obtains the round-trip propagation delay and local clock compensation about the server. The broadcast server regularly sends messages to the specified IP broadcast address or IP multicast address, and usually does not receive requests from clients. The broadcast client listens to the address, but usually does not send a request to the server. Some broadcast servers may choose to respond to client requests and send unsolicited broadcast messages; Meanwhile, some broadcast clients may only send requests to determine the network propagation delay between the server and the client.
In unicast mode, IP addresses of clients and servers are allocated as usual. In broadcast mode, the server uses the specified IP broadcast address or IP multicast address and the specified media access broadcast address, where the client will listen for frames. Therefore, IP broadcast addresses will be limited to a single IP subnet, because routers will not propagate IP broadcast datagrams. For Ethernet, for example, the Ethernet media access broadcast address (all hosts are 1) is used to represent the IP broadcast address.
On the other hand, IP multicast addresses extend the potential effective range of broadcasting to the whole Internet. Its actual scope, group membership and routing are determined by Internet Group Management Protocol (IGMP) [DEE89], and various routing protocols are beyond the scope of this paper. As far as Ethernet is concerned, for example, Ethernet media access broadcast addresses (all of them are 1) should be shared with the assigned IP multicast address 224.0. 1. 1. Except for IP address specification and IGMP, there is no difference between IP broadcast address and IP multicast address running on the server.
The broadcast client listens to the broadcast address, for example, in the case of Ethernet, all host addresses are 1. As far as the IP of the broadcast address is concerned, there is no need to further specify it. In the case of IP multicast, the host may need to implement IGMP so that the local router can intercept the message and send it to the 224.0. 1. 1 multicast group. These considerations are beyond the scope of this information.
As far as SNTP is concerned, its real weakness is that multicast clients may be paralyzed by some misbehaving or hostile SNTP/NTP multicast servers in other parts of the Internet, because all these servers currently use the same IP multicast address: 224.0. 1. 1 group address. Therefore, access control must be based on the source address of the server that the client trusts, that is, the client chooses the server that only he knows. Alternatively, according to customary and informal protocols, all NTP multicast servers should now include encryption bits encrypted with MD5 in each message, so that the client can confirm that the message has not been modified in transmission. In principle, it is possible for SNTP customers to implement those necessary encryption and key distribution plans, but this cannot be considered in those simple systems designed by SNTP.
Considering that there is no complete SNTP specification, IP broadcast address will be used in IP subnet and local area network (referring to the local area network with complete NTP server and SNTP client in the same subnet), while IP multicast address will only be used in special circumstances designed to achieve the same goal. Especially, the IP multicast address in the SNTP server is only used when the server implements NTP authentication described in RFC 1305 (including the algorithm supporting MD5 message bits).
3.NTP timestamp format
Sntp uses the standard ntp timestamp format described in RFC 1305 and its earlier versions. Consistent with Internet standards, NTP data are designated as integers or fixed-point decimals, and are counted from left 0 or high bits in a big-endian manner. Unless not specified, all quantities will be set to unsigned type, and the entire field width can be filled with implied zeros before bit0.
Because SNTP timestamp is important data, which is used to describe the main products of the protocol, a special timestamp format is established. NTP is a 64-bit unsigned fixed point with time stamp, counting from 0: 0: 0 of 1900 65438+ 10/month 1 in seconds. Among the first 32 bits of the integer part, the last 32 bits (fraction of a second) are used to represent the part below a second. In the fraction of seconds section, the meaningless low bit should be set to 0. This format uses a convenient multi-precision algorithm and conversion to represent UDP/TIME (unit: seconds), but it complicates the process of converting it into ICMP timestamp message representation (unit: milliseconds). The accuracy it represents is about 200 picoseconds, which should be enough to meet the highest requirements.
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| seconds |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| seconds part (0 padding) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Note that starting from 1968, the most significant bit (bit 0 of the integer part) has been determined, and the 64-bit field will overflow in 2036. If NTP or SNTP is still used in 2036, some external methods are needed to adjust the time related to 1900 and 2036 (as are other multiples of 136). With these restrictions, timestamp data becomes very special (it needs appropriate methods to find it easily). From now on, there will be an interval of 200 picoseconds every 136, which will be ignored and all 64-bit fields will be set to 0, which will be interpreted as invalid or unavailable timestamps according to the convention.
4.NTP message format
NTP and SNTP are clients of User Datagram Protocol (UDP) [POS80], while UDP itself is clients of Internet Protocol (IP) [DAR8 1]. The structures of IP and UDP headers are described in the cited specific materials, and will not be repeated here. The port of UDP is 123. The source and destination disconnections in the UDP header are the same, and the reserved UDP header is as described in the specification.
The following is a description of the SNTP message format, which follows the IP and UDP headers. The message format of SNTP is consistent with the NTP format described in RFC- 1305, but the difference is that some data fields of SNTP are loaded by the wind, that is, they are initialized to some predetermined values. The format of NTP message is as follows.
1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| LI | VN | Mode | Stratum | Polling | Accuracy |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Root delay
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root difference |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reference Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Reference timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Original timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Accept timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Transmission timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| |
| Authenticator (may