CN114448547A - Time synchronization method and device, electronic equipment and storage medium - Google Patents
Time synchronization method and device, electronic equipment and storage medium Download PDFInfo
- Publication number
- CN114448547A CN114448547A CN202111667867.9A CN202111667867A CN114448547A CN 114448547 A CN114448547 A CN 114448547A CN 202111667867 A CN202111667867 A CN 202111667867A CN 114448547 A CN114448547 A CN 114448547A
- Authority
- CN
- China
- Prior art keywords
- time
- response message
- client
- message
- leaving
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 104
- 230000004044 response Effects 0.000 claims abstract description 158
- 230000008569 process Effects 0.000 claims abstract description 66
- 230000005540 biological transmission Effects 0.000 claims abstract description 25
- 238000012937 correction Methods 0.000 claims abstract description 19
- 238000001514 detection method Methods 0.000 claims description 92
- 238000004590 computer program Methods 0.000 claims description 7
- 230000004048 modification Effects 0.000 claims 1
- 238000012986 modification Methods 0.000 claims 1
- 238000004364 calculation method Methods 0.000 abstract description 3
- 238000010586 diagram Methods 0.000 description 11
- 230000006870 function Effects 0.000 description 11
- 230000003287 optical effect Effects 0.000 description 9
- 238000012545 processing Methods 0.000 description 5
- 238000004891 communication Methods 0.000 description 4
- 238000003491 array Methods 0.000 description 2
- 230000000694 effects Effects 0.000 description 2
- 239000013307 optical fiber Substances 0.000 description 2
- 230000002093 peripheral effect Effects 0.000 description 2
- 230000000644 propagated effect Effects 0.000 description 2
- 239000004065 semiconductor Substances 0.000 description 2
- 238000013519 translation Methods 0.000 description 2
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000006243 chemical reaction Methods 0.000 description 1
- 238000001914 filtration Methods 0.000 description 1
- 101150026195 hook gene Proteins 0.000 description 1
- 230000007246 mechanism Effects 0.000 description 1
- 230000008707 rearrangement Effects 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
- 230000001360 synchronised effect Effects 0.000 description 1
- 238000012546 transfer Methods 0.000 description 1
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04J—MULTIPLEX COMMUNICATION
- H04J3/00—Time-division multiplex systems
- H04J3/02—Details
- H04J3/06—Synchronising arrangements
- H04J3/0635—Clock or time synchronisation in a network
- H04J3/0638—Clock or time synchronisation among nodes; Internode synchronisation
- H04J3/0658—Clock or time synchronisation among packet nodes
- H04J3/0661—Clock or time synchronisation among packet nodes using timestamps
- H04J3/0667—Bidirectional timestamps, e.g. NTP or PTP for compensation of clock drift and for compensation of propagation delays
Landscapes
- Engineering & Computer Science (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Computer And Data Communications (AREA)
Abstract
The embodiment of the invention discloses a time synchronization method, a time synchronization device, electronic equipment and a storage medium. The method comprises the following steps: sending a time setting message of a network time protocol NTP and receiving a response message of the time setting message; acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client; and obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client. Compared with the prior art, the time calculation range of sending the time tick messages from the client to the server and sending the response messages from the server to the client is reduced, and the influence of the random waiting time of the messages from the kernels of the client and the server to the application on the transmission delay is eliminated (the random delay exists from the kernel LOCAL _ IN point to the application and from the kernel LOCAL _ OUT point), so that the time tick precision is improved.
Description
Technical Field
The embodiment of the invention relates to the technical field of time synchronization, in particular to a time synchronization method, a time synchronization device, electronic equipment and a storage medium.
Background
With the continuous emergence of computer network applications, the problem of time synchronization of computers becomes an increasingly important issue. Network Time Protocol (NTP) is a Protocol for Time synchronizing computers, and is used to synchronize Time between distributed Time servers and clients. It can provide high accuracy time correction and can carry authentication code for identity authentication and integrity protection to prevent malicious protocol attacks. The purpose of NTP is to synchronize the clocks of all devices in the network with each other, so that the clocks of all devices in the network are consistent, thereby enabling the devices to provide a variety of applications based on uniform time.
NTP theory of operation: the method comprises the steps that a request is sent to an NTP server through a network interface at the time T1, the NTP server receives the request of the NTP client at the time T3, the NTP server sends a response for receiving the request to the NTP client at the time T2, and then the NTP client receives the response sent by the NTP server at the time T4. In the prior art, when time setting, the time of the NTP client is modified to be: the time of T2+ one-way transmission delay (the time D2 required from the response of the NTP server sending the reception request to the NTP client receiving the response, since D2 is not accurately available, but D1+ D2 can be calculated, and (D1+ D2)/2 can be used instead of D2), the premise that the time synchronization method of this client/server mode is established is that the time D1 and the time D2 required from the NTP client sending the request to the NTP server receiving the request are equal, however, in fact, the time D1 and the time D2 are different, and the accuracy of the time synchronization depends on the degree to which the time D1 and the time D2 are equal, the closer the time D1 and the time D2 are, the higher the accuracy of the time synchronization is, the greater the deviation of the time D1 and the time D2 is, the worse the accuracy of the time is.
Disclosure of Invention
In view of this, embodiments of the present invention provide a time synchronization method, an apparatus, an electronic device, and a storage medium, so as to improve time synchronization accuracy in a client and server mode.
In a first aspect, an embodiment of the present invention provides a time synchronization method, including:
sending a time setting message of NTP and receiving a response message of the time setting message;
acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client;
and obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client.
In a second aspect, an embodiment of the present invention further provides a time synchronization apparatus, including:
the message transmission module is used for sending a network time protocol time setting message and receiving a response message of the time setting message;
the acquisition module is used for acquiring the time of the response message leaving the local machine and calculating the time required by the response message from leaving the local machine to reaching the application process of the client;
and the time synchronization module is used for obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client.
In a third aspect, an embodiment of the present invention further provides an electronic device, including:
one or more processors;
storage means for storing one or more programs;
the one or more programs, when executed by the one or more processors, cause the processors to implement a method of pairing as described in any of the embodiments of the invention.
In a fourth aspect, a computer-readable storage medium has stored thereon a computer program which, when executed by a processor, implements a method of time synchronization as described in any of the embodiments of the invention.
According to the technical scheme of the embodiment of the invention, the time setting message of the NTP is sent and the response message of the time setting message is received; acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client; and obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client. According to the technical scheme, the time tick messages and the timestamps IN the response messages are replaced by the designated detection points IN the Netfilter framework, so that the time of the client can be modified according to the time of the response messages leaving the server at the designated detection points of the server and the time of the response messages leaving the server from the designated detection points of the server to reach the designated detection points of the client and the time required by the application process of the client.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings needed to be used in the embodiments will be briefly described below, it should be understood that the following drawings only illustrate some embodiments of the present invention and therefore should not be considered as limiting the scope, and for those skilled in the art, other related drawings can be obtained according to the drawings without inventive efforts.
Fig. 1 is a flowchart of a time synchronization method according to an embodiment of the present invention;
fig. 2 is a schematic effect diagram of an implementation of a time synchronization method according to an embodiment of the present invention;
fig. 3 is a schematic structural diagram of a time synchronization apparatus according to a second embodiment of the present invention;
fig. 4 is a schematic structural diagram of an electronic device according to a third embodiment of the present invention.
Detailed Description
The present invention will be described in further detail with reference to the accompanying drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the invention and are not limiting of the invention. It should be further noted that, for the convenience of description, only some of the structures related to the present invention are shown in the drawings, not all of the structures. In addition, the embodiments and features of the embodiments in the present invention may be combined with each other without conflict.
Before discussing exemplary embodiments in more detail, it should be noted that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although a flowchart may describe the operations (or steps) as a sequential process, many of the operations can be performed in parallel, concurrently or simultaneously. In addition, the order of the operations may be re-arranged. The process may be terminated when its operations are completed, but may have additional steps not included in the figure. The processes may correspond to methods, functions, procedures, subroutines, and the like. In addition, the embodiments and features of the embodiments in the present invention may be combined with each other without conflict.
The term "include" and variations thereof as used herein are intended to be open-ended, i.e., "including but not limited to". The term "based on" is "based, at least in part, on". The term "one embodiment" means "at least one embodiment".
It should be noted that: like reference numbers and letters refer to like items in the following figures, and thus, once an item is defined in one figure, it need not be further defined and explained in subsequent figures. Meanwhile, in the description of the present invention, the terms "first", "second", and the like are used only for distinguishing the description, and are not to be construed as indicating or implying relative importance.
Example one
Fig. 1 is a flowchart of a time synchronization method according to an embodiment of the present invention, where the present embodiment is applicable to a time synchronization situation in a client and server mode, and the method may be executed by a time synchronization apparatus according to an embodiment of the present invention, where the apparatus may be implemented in a software and/or hardware manner and is integrated in an electronic device, where the electronic device mainly refers to an NTP client. As shown in fig. 1, the method specifically includes the following steps:
s110, sending a time setting message of a network time protocol NTP and receiving a response message of the time setting message.
In the process, the time synchronization can be performed according to various parameters included in the time synchronization message and the response message, and the parameters can include a timestamp of the client when the client sends the time synchronization message, a timestamp of the server when the server receives the time synchronization message, a timestamp of the server when the server sends the response time synchronization message, and the like. The timestamp may be understood as a local time, and for example, when the client sends a message, the timestamp is a local time on the client, and when the server sends a response message, the timestamp is a local time on the server. Of course, the time-alignment message may further include parameters of the operating mode, for example, the operating mode may include a client mode and a server mode.
It should be noted that, for convenience of description, the time required from sending a request (i.e., sending a time tick message) from the NTP client to receiving the request (i.e., receiving the time tick message) by the NTP server may be denoted as D1, and the time required from sending a response (i.e., sending a response message) to receiving a response (i.e., receiving the response message) by the NTP client may be denoted as D2, where D1 or D2 may be understood as a one-way transmission delay of the time tick message or the response message.
Taking the response that the NTP server sends the receive request to the NTP client at time T2 as an example, then: d2 ═ D1 +/Δ D;
T=T2+(D1+D2)/2
=T2+D2/2+D1/2
=T2+D2/2+(D2-△D)/2
=T2+D2-△D/2
=T2+D2-(D2-D1)/2;
the time setting error is (D2-D1)/2, and it can be understood that, in order to improve the proximity of D1 and D2 and further improve the time setting accuracy, a Netfilter framework of the Linux system may be used, for example, the time setting message may be matched at a detection point in the Netfilter framework, and a timestamp in the time setting message may be replaced.
In the embodiment of the present invention, in the aspect of the client, the time synchronization packet of the NTP may be sent to the server, and the response packet returned by the server may also be received, specifically, the time when the client sends the time synchronization packet, that is, the time when the time synchronization packet leaves the client may be a time corresponding to the time when the time synchronization packet leaves the detection point after the time synchronization packet sent by the client reaches the specified detection point in the Netfilter frame of the client, and may be denoted as T1; the time when the client receives the response packet, that is, the time when the response packet reaches the client may be the time when the response packet reaches the specified detection point in the Netfilter frame of the client, and may be denoted as T4.
S120, acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the client application process.
It should be noted that, after receiving the time tick message sent by the client, the server sends a response message of the time tick message, and then the client receives the response message sent by the server, where the response message includes a timestamp of the server when the server sends the response time tick message, so that the time when the response message leaves the local machine, that is, after the response message reaches a specified detection point in the Netfilter frame of the server, the time when the response message leaves the detection point can be recorded as T2. In addition, the random delay time from the server application process to the designated detection point of the server of the response message is eliminated, so that the time synchronization is more accurate.
It will be appreciated that the time required for a response message to arrive at a specified detection point in the Netfilter frame of the client from the specified detection point in the Netfilter frame of the server can be re-denoted as D2, and the time corresponding to the arrival of the response message at the client application process can be denoted as T5, so that the time required for the response message to arrive at the client application process from the specified detection point of the client can be denoted as T5 minus T4, and the time required for the response message to arrive at the client application process from leaving the native machine can be understood as D2 plus T5 minus T4.
S130, obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client.
According to the embodiment of the invention, the correction time of the client can be obtained according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the client application process, for example, the correction time can be represented as T2+ D2+ (T5-T4), so that the local time of the client can be corrected, and the local time of the client and the local time of the server can be synchronized.
According to the technical scheme of the embodiment of the invention, the time setting message of the network time protocol NTP is sent and the response message of the time setting message is received; acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client; and obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client. According to the technical scheme, the time tick messages and the timestamps IN the response messages are replaced by the designated detection points IN the Netfilter framework, so that the time of the client can be modified according to the time of the response messages leaving the server at the designated detection points of the server and the time of the response messages leaving the server from the designated detection points of the server to reach the designated detection points of the client and the time required by the application process of the client.
Further, on the basis of the embodiment of the present invention, the correction time of the client is:
the time for the response message to leave the native machine plus the time required for the response message to arrive at the client application process from leaving the native machine.
In the embodiment of the present invention, the correction time of the client may be represented by the corresponding time when the response packet leaves the specified detection point in the Netfilter frame of the server, plus the time required for the response packet to leave from the specified detection point of the server to reach the specified detection point of the client and then to reach the application process of the client. For example, the correction time may be represented as T2+ D2+ (T5-T4).
Further, on the basis of the above embodiment of the present invention, in the process of sending the time tick message and receiving the response message to the time tick message, the method further includes:
and replacing the time stamps of the time comparison message and the time stamp of the response message at a specified detection point in the Netfilter frame.
The Netfilter framework can be a subsystem introduced by the Linux 2.4.x system, is a structured bottom framework in a Linux system kernel for extending various network services, and can be used as a universal and abstract framework for providing a whole set of management mechanisms of hook functions, so that connection tracking such as data packet filtering, Network Address Translation (NAT) and based on protocol types becomes possible. The Netfilter framework may place several detection points (HOOKs) at several locations throughout the network flow, and may register several processing functions in each detection point to filter or modify packets. These detection points may include PRE _ ROUTING, LOCAL _ IN, FORWARD, POST _ ROUTING, and LOCAL _ OUT detection points. PRE _ ROUTING detection Point: the data packet which just enters the network layer passes through the detection point, the routing judgment is not carried out on the data packet, and the source address conversion is carried out at the point; LOCAL _ IN (LOCAL upload) detection point: after the routing check judgment, if the data packet is sent to the local, the data packet sent to the local passes through the check point; FORWARD detection point: after route checking judgment, if the data packet is not sent to the local, the forwarded packet passes through the detection point; LOCAL _ OUT (off-LOCAL) detection point: the data packet sent by the local process passes through the detection point; POST _ ROUTING detection point: all data packets that need to be sent out through the network device pass through this detection point, where the built-in destination address translation function is performed. If the data packet is sent to the local, the data packet flow direction is as follows: from PRE _ ROUTING detection point to LOCAL _ IN detection point; if the data packet is sent out locally, the flow direction of the data packet is as follows: from the LOCAL _ OUT detection point to the POST _ ROUTING detection point. Therefore, when the client sends the time-tick message, the time stamp in the time-tick message can be replaced at the LOCAL _ OUT detection point of the client; when the server sends a response message, replacing a timestamp of the response message at a LOCAL _ OUT detection point of the server; when the server receives the time-setting message, replacing a timestamp IN the time-setting message at a LOCAL _ IN detection point of the server; and when the client receives the response message, replacing the timestamp of the response message at the LOCAL _ IN detection point of the client.
Further, on the basis of the above embodiment of the present invention, the time stamping of the time tick message and the time stamp of the response message includes:
a first timestamp corresponding to a detection point when the time synchronization message leaves the local machine;
a third timestamp corresponding to the detection point sent by the time tick message on the local machine;
responding to a second timestamp corresponding to the detection point of the message leaving the local computer;
responding to the detection point sent by the message on the local computer and corresponding to the fourth timestamp;
and detecting a fifth timestamp corresponding to the detection point of the response message in the client application process.
IN the embodiment of the present invention, when a time tick message leaves a LOCAL _ OUT detection point of a client, a corresponding time is a first timestamp (denoted as T1), when the time tick message reaches a LOCAL _ IN detection point on a server, a corresponding time is a third timestamp (denoted as T3), when a response message leaves the LOCAL _ OUT detection point on the server, a corresponding time is a second timestamp (denoted as T2), when the response message reaches the LOCAL _ IN detection point of the client, a corresponding time is a fourth timestamp (denoted as T4), and when the response message detects a client application process, a corresponding time is a fifth timestamp (denoted as T5).
Further, on the basis of the above embodiment of the present invention, the time required for the response packet to arrive at the client application process from leaving the native device includes:
the one-way transmission delay of the response message from off-native to native, and the time required for the response message to arrive at the client application process from native.
IN the embodiment of the present invention, the one-way transmission delay from the LOCAL _ OUT detection point of the server to the LOCAL _ IN detection point of the client is a response packet, where the one-way transmission delay may include a delay from the LOCAL _ OUT detection point of the server to the network interface of the server plus a network transmission delay (a delay from the network interface of the server to the network interface of the client) plus a delay from the network interface of the client to the LOCAL _ IN detection point of the client, and a time required by the response packet from the LOCAL _ IN detection point of the client to the application process reaching the client.
Further, on the basis of the above embodiment of the present invention, the one-way transmission delay is 1/2 of a difference between a first time period and a second time period, where the first time period is a difference obtained by subtracting the first timestamp from the fourth timestamp, and the second time period is a difference obtained by subtracting the third timestamp from the second timestamp; the time required for the response message to be sent from the local machine to reach the client application process is the difference value obtained by subtracting the fourth time stamp from the fifth time stamp.
It should be noted that, if the delay from the sending delay part, i.e. from the application process to the LOCAL _ OUT detection point, and the delay from the receiving delay part, i.e. from the LOCAL _ IN detection point to the application process, are excluded, the delay from the sending end LOCAL _ OUT detection point to the network interface plus the delay from the network transmission delay plus the delay from the receiving end network interface to the LOCAL _ IN detection point may constitute a one-way transmission delay, i.e. a one-way transmission delay from the off-LOCAL to the LOCAL of the response packet, or a one-way transmission delay from the off-LOCAL to the LOCAL of the time packet to the client, so the one-way transmission delay may be represented by (the difference between the fourth timestamp minus the first timestamp) minus (the difference between the second timestamp minus the third timestamp) minus a value of 2, which may be represented as ((T4-T1) - (T2-T3))/2, i.e. 1/2, which is the difference between the first time period and the second time period, the first time interval is a difference value obtained by subtracting the first time stamp from the fourth time stamp, and the second time interval is a difference value obtained by subtracting the third time stamp from the second time stamp.
IN the embodiment of the present invention, the time required for the response packet to reach the application process of the client from the LOCAL _ IN detection point of the client may be represented as a difference obtained by subtracting the fourth timestamp from the fifth timestamp (i.e., T5-T4).
Further, on the basis of the above embodiment of the present invention, after replacing the timestamps of the time tick message and the response message, the method further includes:
and updating the message authentication code corresponding to the time tick message.
It should be noted that, because the timestamp of the time tick message and the response message is replaced at the designated detection point in the Netfilter frame, the message authentication code corresponding to the time tick message can be updated in order that the time tick message received by the server and the response message received by the client are valid messages. Specifically, after a first timestamp is recorded at a detection point of a client of a time synchronization message, a message authentication code needs to be recalculated, and when a server receives the time synchronization message and performs message authentication, the server can clear the first timestamp recorded by the client and then performs message authentication; correspondingly, the client performs message authentication after clearing the second timestamp recorded by the server.
Exemplarily, for convenience of understanding, referring to fig. 2, fig. 2 is a schematic effect diagram of an implementation of a time synchronization method according to an embodiment of the present invention.
The time tick message 212 sent by the NTP client 201 is sent from the application process 202 of the NTP client 201 to the LOCAL _ OUT detection point 204 of the NTP client 201, at this time, the time when the time tick message 212 reaches the LOCAL _ OUT detection point 204 is recorded as a first time stamp T1, then the time tick message 212 is sent from the LOCAL _ OUT detection point 204 to the NTP client network interface 206, then the time tick message 212 is transmitted from the NTP client network interface 206 to the NTP server network interface 211, then the time tick message 212 is transmitted from the NTP server network interface 211 to the LOCAL _ IN detection point 210 of the NTP server 207, at this time, the time when the time tick message 212 reaches the LOCAL _ IN detection point 210 is recorded as a third time stamp T3, and finally, the time tick message 212 is sent from the LOCAL _ IN detection point 210 to the application process 208 of the NTP server 207. Additionally, the boundary 203 is the boundary of the application process to the kernel.
Sending the response packet 213 from the application process 208 of the NTP server 207 to the LOCAL _ OUT detection point 209 of the NTP server 207, where the time when the response packet 213 reaches the LOCAL _ OUT detection point 209 is denoted as a second timestamp T2, then the response packet 213 reaches the NTP server network interface 211 from the LOCAL _ OUT detection point 209, then the response packet 213 is transmitted from the NTP server network interface 211 to the NTP client network interface 206, then the response packet 213 reaches the LOCAL _ IN detection point 205 of the NTP client 201 from the NTP client network interface 206, where the time when the response packet 213 reaches the LOCAL _ IN detection point 205 of the NTP client 201 is denoted as a fourth timestamp T4, and finally the response packet 213 reaches the application process 202 of the NTP client 201 from the LOCAL _ IN detection point 205, where the time when the response packet 213 reaches the application process 202 is denoted as a fifth timestamp T5.
As shown in fig. 2, moving NTP timestamping down into the kernel excludes the random latency of the transmit delay portion and the receive delay portion from the computation path. Thus, the transmission delay includes the following components:
the delay from client LOCAL _ OUT to network interface section + one-way transmission delay + the delay from server network interface to LOCAL _ IN section.
The calculation formula of the correction time at the client is as follows:
T=T2+D2+(T5-T4)
=T2+D/2+(T5-T4)
=T2+((T4-T1)-(T2-T3))/2+(T5-T4)
at the client, the time delay of sending the request to the LOCAL _ OUT is excluded from the calculation path, and when the response message is received, the random waiting time from the LOCAL _ IN to the application program can be accurately compensated by using (T5-T4); at the server, a random part of receiving the request LOCAL _ IN to the application processing part and sending the response to LOCAL _ OUT is included IN (T2-T3). On the basis, the random part in the time delay path is eliminated, and the time synchronization precision of the client and the server NTP is improved.
Example two
Fig. 3 is a schematic structural diagram of a time synchronization device according to a second embodiment of the present invention. The embodiment may be applicable to the time setting situation in the client and server modes, the apparatus may be implemented in a software and/or hardware manner, and the apparatus may be integrated in any device providing a time setting function, as shown in fig. 3, where the time setting apparatus specifically includes: a message transmission module 310, an acquisition module 320 and a time synchronization module 330.
The message transmission module 310 is configured to send a network time protocol time tick message and receive a response message of the time tick message;
an obtaining module 320, configured to obtain time for the response packet to leave the local machine, and calculate time required for the response packet to reach the client application process from leaving the local machine;
the time synchronization module 330 is configured to obtain the correction time of the client according to the time when the response packet leaves the local computer and the time required by the response packet from leaving the local computer to reaching the client application process.
According to the technical scheme of the embodiment of the invention, the message transmission module is used for sending the time setting message of the NTP and receiving the response message of the time setting message; acquiring the time of the response message leaving the local machine through an acquisition module, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client; and obtaining the correction time of the client by the time synchronization module according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client. According to the technical scheme, the time setting message and the time stamp in the response message are replaced by the designated detection point in the Netfilter frame, so that the time of the client can be modified according to the time of the response message leaving the server at the designated detection point of the server and the time of the response message leaving the server from the designated detection point of the server to the designated detection point of the client and then reaching the client application process.
Further, on the basis of the above embodiment of the present invention, the correction time of the client is:
the time of the response message leaving the local machine plus the time required for the response message to arrive at the client application process from leaving the local machine.
Further, on the basis of the above embodiment of the present invention, the message transmission module 310 includes a replacing unit:
and the replacing unit is used for replacing the time setting message and the timestamp of the response message at a specified detection point in the Netfilter frame in the process of sending the time setting message and receiving the response message of the time setting message.
Further, on the basis of the above embodiment of the present invention, the time stamp of the time tick message and the time stamp of the response message include:
a first timestamp corresponding to a detection point when the time tick message leaves the local machine;
a third timestamp corresponding to the detection point sent by the time tick message on the local machine;
a second timestamp corresponding to the detection point of the response message leaving the local computer;
the detection point sent by the response message on the local computer corresponds to a fourth timestamp;
and detecting a fifth timestamp corresponding to the detection point of the response message in the client application process.
Further, on the basis of the above embodiment of the present invention, the time required for the response packet to arrive at the client application process after leaving the native device includes:
the one-way transmission delay of the response message from leaving the native machine to being sent up to the native machine, and the time required for the response message to be sent up to the client application process from the native machine.
Further, on the basis of the above embodiment of the present invention, the one-way transmission delay is 1/2 of a difference between a first time period and a second time period, where the first time period is a difference obtained by subtracting the first timestamp from the fourth timestamp, and the second time period is a difference obtained by subtracting the third timestamp from the second timestamp;
and the time required by the response message from the local machine to the client application process is the difference obtained by subtracting the fourth timestamp from the fifth timestamp.
Further, on the basis of the foregoing embodiment of the present invention, after replacing the timestamps of the pair time packet and the response packet, the replacing unit is further configured to:
and updating the message authentication code corresponding to the time tick message.
The product can execute the method provided by any embodiment of the invention, and has corresponding functional modules and beneficial effects of the execution method.
EXAMPLE III
Fig. 4 is a schematic structural diagram of an electronic device according to a third embodiment of the present invention. FIG. 4 illustrates a block diagram of an electronic device 412 suitable for use in implementing embodiments of the present invention. The electronic device 412 shown in fig. 4 is only an example and should not bring any limitations to the functionality and scope of use of the embodiments of the present invention. Device 412 is a typical time-tick computing device.
As shown in fig. 4, the electronic device 412 is in the form of a general purpose computing device. The components of the electronic device 412 may include, but are not limited to: one or more processors 416, a storage device 428, and a bus 418 that couples the various system components including the storage device 428 and the processors 416.
The electronic device 412 may also communicate with one or more external devices 414 (e.g., keyboard, pointing device, camera, display 424, etc.), with one or more devices that enable a user to interact with the electronic device 412, and/or with any devices (e.g., network card, modem, etc.) that enable the electronic device 412 to communicate with one or more other computing devices. Such communication may occur via input/output (I/O) interfaces 422. Also, the electronic device 412 may communicate with one or more networks (e.g., a Local Area Network (LAN), Wide Area Network (WAN), and/or a public Network, such as the internet) via the Network adapter 420. As shown, network adapter 420 communicates with the other modules of electronic device 412 over bus 418. It should be appreciated that although not shown in the figures, other hardware and/or software modules may be used in conjunction with the electronic device 412, including but not limited to: microcode, device drivers, Redundant processing units, external disk drive Arrays, disk array (RAID) systems, tape drives, and data backup storage systems, to name a few.
The processor 416 executes programs stored in the storage 428 to perform various functional applications and data processing, such as implementing the time synchronization methods provided by the above-described embodiments of the present invention.
Example four
A fourth embodiment of the present invention provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processing apparatus, the computer program implements a time synchronization method as in the embodiment of the present invention. The computer readable medium of the present invention described above may be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present disclosure, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In contrast, in the present disclosure, a computer readable signal medium may comprise a propagated data signal with computer readable program code embodied therein, either in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, optical cables, RF (radio frequency), etc., or any suitable combination of the foregoing.
In some embodiments, the clients, servers may communicate using any currently known or future developed network Protocol, such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), the Internet (e.g., the Internet), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future developed network.
The computer readable medium may be embodied in the electronic device; or may exist separately without being assembled into the electronic device.
The computer readable medium carries one or more programs which, when executed by the electronic device, cause the electronic device to: sending a time setting message of NTP and receiving a response message of the time setting message;
acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client;
and obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client.
Computer program code for carrying out operations for the present disclosure may be written in any combination of one or more programming languages, including but not limited to an object oriented programming language such as Java, Smalltalk, C + +, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units described in the embodiments of the present disclosure may be implemented by software or hardware. Where the name of an element does not in some cases constitute a limitation on the element itself.
The functions described herein above may be performed, at least in part, by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field Programmable Gate Arrays (FPGAs), Application Specific Integrated Circuits (ASICs), Application Specific Standard Products (ASSPs), systems on a chip (SOCs), Complex Programmable Logic Devices (CPLDs), and the like.
In the context of this disclosure, a machine-readable medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
It is to be noted that the foregoing is only illustrative of the preferred embodiments of the present invention and the technical principles employed. It will be understood by those skilled in the art that the present invention is not limited to the particular embodiments described herein, but is capable of various obvious changes, rearrangements and substitutions as will now become apparent to those skilled in the art without departing from the scope of the invention. Therefore, although the present invention has been described in greater detail by the above embodiments, the present invention is not limited to the above embodiments, and may include other equivalent embodiments without departing from the spirit of the present invention, and the scope of the present invention is determined by the scope of the appended claims.
Claims (10)
1. A method of time synchronization, comprising:
sending a time setting message of a network time protocol NTP and receiving a response message of the time setting message;
acquiring the time of the response message leaving the local machine, and calculating the time required by the response message from leaving the local machine to reaching the application process of the client;
and obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client.
2. The method of claim 1, wherein the modification time of the client is:
the time of the response message leaving the local machine plus the time required for the response message to arrive at the client application process from leaving the local machine.
3. The method according to claim 1, wherein in the process of sending a time tick message and receiving a response message of the time tick message, the method further comprises:
and replacing the time stamps of the time comparison message and the response message at a specified detection point in the Netfilter frame.
4. The method of claim 3, wherein the time stamping the time tick message and the response message comprises:
a first timestamp corresponding to a detection point when the time tick message leaves the local machine;
a third timestamp corresponding to the detection point sent by the time tick message on the local machine;
a second timestamp corresponding to the detection point of the response message leaving the local computer;
the detection point sent by the response message on the local computer corresponds to a fourth timestamp;
and detecting a fifth timestamp corresponding to the detection point of the response message in the client application process.
5. The method of claim 4, wherein the time required for the response message to travel from leaving the native device to reaching the client application process comprises:
the one-way transmission delay of the response message from leaving the native machine to being sent up to the native machine, and the time required for the response message to be sent up to the client application process from the native machine.
6. The method of claim 5,
the one-way transmission delay is 1/2 of a difference between a first time period and a second time period, wherein the first time period is a difference obtained by subtracting the first timestamp from the fourth timestamp, and the second time period is a difference obtained by subtracting the third timestamp from the second timestamp;
and the time required by the response message from the local machine to the client application process is the difference obtained by subtracting the fourth timestamp from the fifth timestamp.
7. The method of claim 3, after replacing the time stamps of the time tick messages and the response messages, further comprising:
and updating the message authentication code corresponding to the time tick message.
8. A time synchronization apparatus, comprising:
the message transmission module is used for sending a network time protocol time setting message and receiving a response message of the time setting message;
the acquisition module is used for acquiring the time of the response message leaving the local machine and calculating the time required by the response message from leaving the local machine to reaching the application process of the client;
and the time synchronization module is used for obtaining the correction time of the client according to the time of the response message leaving the local machine and the time of the response message from leaving the local machine to reaching the application process of the client.
9. An electronic device, comprising:
one or more processors;
storage means for storing one or more programs;
the one or more programs, when executed by the one or more processors, cause the processors to implement the method of any of claims 1-7.
10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the method of time synchronization according to any one of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111667867.9A CN114448547A (en) | 2021-12-31 | 2021-12-31 | Time synchronization method and device, electronic equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111667867.9A CN114448547A (en) | 2021-12-31 | 2021-12-31 | Time synchronization method and device, electronic equipment and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN114448547A true CN114448547A (en) | 2022-05-06 |
Family
ID=81366096
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202111667867.9A Pending CN114448547A (en) | 2021-12-31 | 2021-12-31 | Time synchronization method and device, electronic equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN114448547A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115865760A (en) * | 2022-12-16 | 2023-03-28 | 苏州浪潮智能科技有限公司 | Network card time delay test method and device |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106992833A (en) * | 2017-05-21 | 2017-07-28 | 华中科技大学 | A kind of ultraviolet communication network clock synchronization system and method |
CN108322281A (en) * | 2018-05-14 | 2018-07-24 | 上海市计量测试技术研究院 | Time calibration in network service based on NTP and assessment system and method |
-
2021
- 2021-12-31 CN CN202111667867.9A patent/CN114448547A/en active Pending
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106992833A (en) * | 2017-05-21 | 2017-07-28 | 华中科技大学 | A kind of ultraviolet communication network clock synchronization system and method |
CN108322281A (en) * | 2018-05-14 | 2018-07-24 | 上海市计量测试技术研究院 | Time calibration in network service based on NTP and assessment system and method |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115865760A (en) * | 2022-12-16 | 2023-03-28 | 苏州浪潮智能科技有限公司 | Network card time delay test method and device |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US9229832B2 (en) | Time monitor | |
EP2448168A1 (en) | Method and system for bearing time synchronization protocol in optical transport network | |
CN111800213B (en) | High-speed TTE (time to live) cascade network 1588 synchronization method, system and device | |
US10078559B2 (en) | System and method for input data fault recovery in a massively parallel real time computing system | |
US7577169B1 (en) | Time stamping | |
US20140257730A1 (en) | Bandwidth and time delay matching for inertial sensors | |
CN113992469B (en) | Data fusion method and device, electronic equipment and computer readable medium | |
US8788689B2 (en) | Techniques for improved clock offset measuring | |
CN111309747A (en) | Data synchronization method, system and device | |
WO2015131626A1 (en) | Time synchronization method and apparatus for network devices and time synchronization server | |
US20220360350A1 (en) | Method and apparatus for acquiring timestamp of data stream, storage medium, and electronic apparatus | |
JP2009182659A (en) | Timing synchronizing method, synchronization device, synchronization system, and synchronization program | |
CN114448547A (en) | Time synchronization method and device, electronic equipment and storage medium | |
CN113067750A (en) | Bandwidth measuring method and device and electronic device | |
CN103957258A (en) | Trans-regional data communication method and system | |
US8837473B2 (en) | Single timestamp engine for generating timing information for inclusion in packets complying with multiple networking protocols | |
CN107003691B (en) | Techniques for synchronously sampling counters based on global clock | |
CN112311619B (en) | Network message delay detection method and device and electronic equipment | |
CN112600641A (en) | Network equipment time synchronization method, device, equipment and storage medium | |
CN110750424B (en) | Resource inspection method and device | |
CN112015746B (en) | Data real-time processing method, device, medium and electronic equipment | |
CN110928955B (en) | Data interaction method and device, computer equipment and storage medium | |
JP2007163330A (en) | Time information communication system | |
Traub et al. | SENSE: Scalable data acquisition from distributed sensors with guaranteed time coherence | |
CN115695587A (en) | Service data processing system, method, device and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |