A kind of multi-scheme obtains the method and system of network time parallel
Technical field
The invention belongs to a kind of methods that network communication technology field more particularly to multi-scheme obtain network time parallel
And system.
Background technique
Internet of Things is the important component of generation information technology, and the important development stage in " informationization " epoch.
IoT (Internet Of Things, Internet of Things) equipment is more and more abundant at present, but overwhelming majority IoT equipment is without power supply
Chip, cannot itself direct proof time, need to correspond to the server of transport layer by the protocol access of transport layer come when obtaining
Between.
Application No. is the domestic patents of invention of CN201710167687.1 to disclose a kind of network time acquisition methods, dress
It sets and system, readable medium and storage control, the method for specifically disclosing includes: to preset at least two ntp servers
Address;S1: from each ntp server address, determine that at least one can get from corresponding NTP time server
The available ntp server address of network time;S2: the NTP time corresponding from each available ntp server address is determined respectively
Acquisition duration needed for getting network time on server;S3: when determining most short acquisition duration, and marking with most short acquisition
Long corresponding available NTP server address;S4: according to predetermined period from the corresponding NTP in available ntp server address of label
Network time is obtained on time server, executes S5;S5: judging whether to get network time, if so, S4 is executed, it is no,
Successively execute S1, S2, S3.The technical solution can be relatively reliable get network time.
But the IoT equipment time acquisition modes of mainstream are to default the NTP (Network used using system bottom at present
Time Protocol, Network Time Protocol) agreement, but most of NTP Server (network time synchronization server) are all provided with
It sets at abroad, not being local self-built server, so part operator route may shield NTP Server or udp
(User Datagram Protocol, User Datagram Protocol) 123 ports (port that transport layer udp protocol uses, port
Number for 123), the time cannot be successfully got when so as to cause IoT equipment from NTP Server acquisition time, thus further
The application of having time stamp verification is influenced, such as: when intelligent sound box plays music, part music sources provider can checking time
Stamp can not then play music once it is not right the sound box system time occur.
So we need to design it is a kind of can effective guarantee different operators route can succeed and promptly obtain
The method of network time.
Summary of the invention
In view of the problems of the existing technology the present invention, proposes a kind of method that multi-scheme obtains network time parallel
And system.
The technical scheme is that:
A kind of method that multi-scheme obtains network time parallel, comprising the following steps:
S1, parallel to time synchronization server, hypertext transfer protocol server issue acquisition time request and to
Message queue telemetering transmission server issues the request for subscribing to time theme;
S2, waiting for server response obtain first network time that the time synchronization server is replied and/or described
The second network time and/or message queue telemetering transmission service that hypertext transfer protocol server is replied reply the
Three network times;
S3, by application condition algorithm from the first network time and/or second network time and/or described
Third network time fall into a trap point counting be precipitated the optimum network time be considered as the grid time.
The technical program has the beneficial effect that
To the time synchronization server, the hypertext transfer protocol server and described disappeared by parallel form
Breath three kinds of servers of queue telemetering transmission server are simultaneously emitted by the request of acquisition time, wait the response of each server, increase
The probability of reply is received, while if application condition calculation can also be passed therethrough when receiving the reply of more than one server
Method therefrom calculates the system time for analyzing the optimum network time as internet of things equipment, and energy effective guarantee is in different operators
Route can succeed and promptly obtain network time.
As the preferred of the technical program, the step S2 includes:
And count the number of servers of respond request.
As the preferred of the technical program, the step S3 includes:
S3.1 judges whether an only server respond request, if the determination result is YES, then directly the server
The network time of reply is calculated as the grid time;If judging result is no, execution step S3.2;
The step S3.2 judges whether that only two-server respond request if the determination result is YES then compares two
The network time that platform server is replied, a kind of turnaround time shorter network time is calculated as system time;If judging result is
It is no, then directly hold step S3.3;
S3.3 counts the first network time, second network time and the third network time two-by-two
It calculates, the first network time, second network time and the third network time three is obtained according to application condition algorithm
It is calculated among person and analyzes the optimum network time and be considered as the grid time.
As the preferred of the technical program, the step S3.2 includes:
S3.21 judges whether only two-server respond request, if judging result be it is no, directly execute described in
Step S3.3;
S3.22, judges whether the time interval between two kinds of network times is less than prefixed time interval, if judging result
Be it is no, then choosing a kind of network time replied at first is considered as system time;If the determination result is YES, then access time stabs more
A kind of big network time is considered as system time.
As the preferred of the technical program, the step S3.3 includes:
S3.31 was compared among the first network time, second network time and the third network time
A kind of timestamp maximum network time;
S3.32 chooses any network time and the maximum network of timestamp in addition to timestamp maximum network time
Time is compared, and judges whether the time interval between two kinds of network times is less than prefixed time interval, if judging result is
No, then choosing the network time first replied is considered as system time;If the determination result is YES, S3.33 is thened follow the steps;
The step S3.23, chooses remaining finally a kind of network time and timestamp maximum network time is compared
Compared with, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result be it is no, choose
A kind of network time first replied is considered as system time;If the determination result is YES, S3.34 is thened follow the steps
The step S3.24 chooses remaining two kinds of network times in addition to timestamp maximum network time and is compared
Compared with, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result be it is no, choose
A kind of network time first replied is considered as system time;If the determination result is YES, then access time stamp teaches big network time view
For system time.
A kind of system that multi-scheme obtains network time parallel, comprising:
Parallel requests module, when being obtained for being issued parallel to time synchronization server, hypertext transfer protocol server
Between request and to message queue telemetering transmission server issue subscribe to time theme request;
Module is obtained, is responded for waiting for server, the first network time that the time synchronization server is replied is obtained
And/or the second network time and/or the message queue telemetering transmission service of the hypertext transfer protocol server reply
The third network time of reply;
Computation analysis module, for passing through application condition algorithm from the first network time and/or second network
Time and/or the third network time fall into a trap point counting be precipitated the optimum network time be considered as the grid time.
As the preferred of the technical program, the acquisition module includes:
Statistic unit, for counting the number of servers of respond request.
As the preferred of the technical program, the computation analysis module includes:
Judging unit, for judging whether an only server respond request, if the determination result is YES, then direct handle
The network time that the server is replied is calculated as the grid time;If judging result is no, execution the first calculating analytical unit;
Described first calculates analytical unit, for judging whether only two-server respond request, if judging result is
It is the network time for then comparing two-server reply, the optimum network time is analyzed according to application condition algorithm and is considered as system
Network time;If judging result be it is no, directly execute second calculate analytical unit;
Described second calculates analytical unit, for the first network time, second network time and described the
Three network times were calculated two-by-two, obtained the first network time, second network time according to application condition algorithm
It analyzes the optimum network time with being calculated among the third network time three and is considered as the grid time.
As the preferred of the technical program, the first calculating analytical unit includes:
First judgment sub-unit, for judging whether only two-server respond request, if judging result be it is no,
It then directly executes described second and calculates analytical unit;
Whether the second judgment sub-unit, the time interval for judging between two kinds of network times are less than between preset time
Every, if judging result be it is no, choosing a kind of network time replied at first is considered as system time;If the determination result is YES, then
A kind of access time stamp bigger network time is considered as system time.
As the preferred of the technical program, the second calculating analytical unit includes:
Comparing subunit, for comparing the first network time, second network time and the third network
Timestamp maximum a kind of network time among time;
Third judgment sub-unit, for choose in addition to timestamp maximum network time any network time, with the time
Stabbing maximum network time is compared, and judges whether the time interval between two kinds of network times is less than prefixed time interval,
If judging result be it is no, choosing the network time first replied is considered as system time;If the determination result is YES, then the 4th is executed
Judgment sub-unit;
4th judgment sub-unit, when for choosing a kind of remaining last network time and the maximum network of timestamp
Between be compared, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result is
No, then choosing the network time first replied is considered as system time;If the determination result is YES, then the 5th judgment sub-unit is executed;
5th judgment sub-unit, for choosing in addition to timestamp maximum network time when remaining two kinds of networks
Between be compared, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result is
No, then choosing the network time first replied is considered as system time;If the determination result is YES, then access time stabs biggish network
Time is considered as system time.
The technical program has the beneficial effect that through time synchronization server, the hypertext described in parallel requests module
Transfer protocol server and described three kinds of servers of message queue telemetering transmission server are simultaneously emitted by the request of acquisition time,
The response that module receives each server is obtained, computation analysis module passes through application condition algorithm from the all-network time received
System time of the optimum network time as internet of things equipment is precipitated in point counting of falling into a trap, and energy effective guarantee is in different operators route
It can succeed and promptly obtain network time.
Detailed description of the invention
Fig. 1 is the flow chart for the method that a kind of multi-scheme of the present invention obtains network time parallel;
Fig. 2 is the specific flow chart of step S3 in the present invention;
Fig. 3 is the specific flow chart of step S3.1 in the present invention;
Fig. 4 is the specific flow chart of step S3.2 in the present invention;
Fig. 5 is the block diagram for the system that a kind of multi-scheme of the present invention obtains network time parallel;
Fig. 6 is the specific block diagram of computation analysis module in the present invention;
Fig. 7 is the first specific block diagram for calculating analytical unit in the present invention;
Fig. 8 is the second specific block diagram for calculating analytical unit in the present invention.
Specific implementation method
In order to make the objectives, technical solutions, and advantages of the present invention clearer, with reference to the accompanying drawings and embodiments,
The present invention will be described in further detail.It should be appreciated that described herein, specific examples are only used to explain the present invention,
It is not intended to limit the present invention.Based on the embodiments of the present invention, those of ordinary skill in the art are not making creative labor
Every other embodiment obtained under the premise of dynamic, shall fall within the protection scope of the present invention.In addition, between each embodiment
Technical solution can be combined with each other, but must based on can be realized by those of ordinary skill in the art, work as technical solution
Combination there is conflicting or cannot achieve when and will be understood that the combination of this technical solution is not present, also not in the present invention
It is required that protection scope within.
Following is a specific embodiment of the present invention in conjunction with the accompanying drawings, technical scheme of the present invention will be further described,
However, the present invention is not limited to these examples.
Embodiment 1
The NTP association that most of IoT (Internet Of Things, Internet of Things) equipment is used using system bottom default
Clock synchronization is discussed, but in part operator route shielding NTP Server (network time synchronization server) or udp (User
Datagram Protocol, User Datagram Protocol) (port that transport layer udp protocol uses, port numbers are for 123 ports
123), so IoT equipment can not normally get network time under certain carrier network environment.
Now in order to solve the above problem, a kind of method that multi-scheme obtains network time parallel is devised.
As shown in Figure 1, obtaining the flow chart of the method for network time parallel for a kind of multi-scheme of the present invention.
A kind of method that multi-scheme obtains network time parallel, comprising the following steps:
S1, parallel to time synchronization server, hypertext transfer protocol server issue acquisition time request and to
Message queue telemetering transmission server issues the request for subscribing to time theme.
S2, waiting for server response obtain first network time that the time synchronization server is replied and/or described
The second network time and/or message queue telemetering transmission service that hypertext transfer protocol server is replied reply the
Three network times.
S3, by application condition algorithm from the first network time and/or second network time and/or described
Third network time fall into a trap point counting be precipitated the optimum network time be considered as the grid time.
The technical program is advantageous in that:
By issuing time request to a variety of servers parallel, the probability for obtaining network event is improved, it is ensured that IoT is set
It is standby can be so getting network time under carrier network.
Nowadays, the intelligent electronic devices such as mobile phone, plate and set-top box can provide such as be broadcast live, review and program request memory
Etc. functions, but then need to be based on current network time, so intelligent electronic device also needs to obtain net when realizing these functions
The network time.But some IoT equipment itself must carry out network by server without power supply chip mostly now
The acquisition of time, such as application No. is one kind of the patent disclosure of CN201410710632.7 to be based on NTP under the prior art
The time synchronization method and time synchronization system of (Network Time Protocol, Network Time Protocol) network, mainly pass through setting
Unique ntp server address, obtain network time from the corresponding NTP time server in the address.
But since ntp server patented technology is mostly foreign patent, the country does not have ntp server, so some are runed
The chamber of commerce selects the scheme for directly abandoning NTP network clock synchronization, and IoT equipment clock synchronization is caused to fail, to further influence IoT equipment
The application of middle having time stamp verification, such as: when intelligent sound box plays music, part music sources provider meeting checking time stamp, one
It is not right that the sound box system time occurs in denier, then can not play music.
In this programme other than the clock synchronization scheme based on NTP network, the side to other two server clock synchronization is also added
Case.It is the scheme to HTTP (HyperText Transfer Protocol, hypertext transfer protocol) server clock synchronization respectively
With to the side of MQTT (Message Queuing Telemetry Transport, message queue telemetering transmission) server clock synchronization
Case.Since HTTP server and MQTT server can be self-built, and MQTT is IBM (IT company-International Business Machine Corporation (IBM))
One instant communication protocol of exploitation is the important component of Internet of Things, which supports all platforms, almost can be
All networking articles and external connection are got up, and are used to as sensor and brake.So selection makes for operator
It is much smaller with the opposite selection NTP network server of the use cost of HTTP server and MQTT server, more transported
Battalion quotient receives and also more mainstream.So increasing this two kinds of networks pair outside clock synchronization scheme of the script based on NTP network
When scheme.
In addition, at the same moment, multiple programs are simultaneously in a parallel fashion to a variety of server request times
It executes, the overlapping in having time, guarantees that network time obtains the accuracy of result.As long as Service-Port normal use, just can
The request for receiving acquisition time simultaneously, then gives and replys.
But the request that every kind of server receives acquisition time is given again replys, and is required to that one can be consumed by processing
A little times, the network time for causing every kind of server to reply is different, and design at this time is obtained by application condition algorithm from all
Network time falls into a trap the point counting precipitation optimum network time, is finally considered as the system network of internet of things equipment with the optimum network time
The network time.
Embodiment 2
Based on embodiment 1, difference from Example 1 is:
As Figure 1-Figure 4, the general flow chart and tool of the method for network time are obtained parallel for a kind of multi-scheme of the present invention
Body flow chart.
S1, parallel to time synchronization server, hypertext transfer protocol server issue acquisition time request and to
Message queue telemetering transmission server issues the request for subscribing to time theme.
The time that request issues is recorded simultaneously.
S2, waiting for server response obtain first network time that the time synchronization server is replied and/or described
The second network time and/or message queue telemetering transmission service that hypertext transfer protocol server is replied reply the
Three network times.
The step S2 includes:
Count the number of servers of respond request.
S3, by application condition algorithm from the first network time and/or second network time and/or described
Third network time fall into a trap point counting be precipitated the optimum network time be considered as the grid time.
The step S3 includes:
S3.1 judges whether an only server respond request, if the determination result is YES, then directly the server
The network time of reply is calculated as the grid time;If judging result is no, execution step S3.2;
The step S3.2 judges whether that only two-server respond request if the determination result is YES then compares two
The network time that platform server is replied analyzes the optimum network time according to application condition algorithm and is considered as the grid time;If
Judging result be it is no, then directly hold step S3.3;
The step S3.2 includes:
S3.21 judges whether only two-server respond request, if judging result be it is no, directly execute described in
Step S3.3.
S3.22, judges whether the time interval between two kinds of network times is less than prefixed time interval, if judging result
Be it is no, then choosing a kind of network time replied at first is considered as system time;If the determination result is YES, then access time stabs more
A kind of big network time is considered as system time.
The step S3.3, to the first network time, second network time and the third network time two
Two are calculated, and obtain the first network time, second network time and the third net according to application condition algorithm
It is calculated among network time three and analyzes the optimum network time and be considered as the grid time.
The step S3.3 includes:
S3.31 was compared among the first network time, second network time and the third network time
A kind of timestamp maximum network time.
S3.32, when choosing any one network time and the maximum network of timestamp in addition to timestamp maximum network time
Between be compared, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result is
No, then choosing a kind of network time most replied is considered as system time;If the determination result is YES, S3.33 is thened follow the steps.
The step S3.23, chooses remaining finally a kind of network time and timestamp maximum network time is compared
Compared with, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result be it is no, choose
A kind of network time first replied is considered as system time;If the determination result is YES, S3.34 is thened follow the steps.
The step S3.24 chooses remaining two kinds of network times in addition to timestamp maximum network time and is compared
Compared with, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result be it is no, choose
The network time first replied is considered as system time;If the determination result is YES, then a kind of access time stamp religion big network time regards
For system time.
If three server responses, obtain the first network time, second network time and the third network
Time (is followed successively by t1, t2 and t3 according to time order and function, and t2 timestamp is maximum), obtains described the according to application condition algorithm
It is calculated among one network time, second network time and the third network time three and analyzes the optimum network time
It is considered as the grid time:
Wherein, prefixed time interval is set as 5S, is primarily due in GPRS (General Packet Radio
Service, general packet radio service technology)/CDMA (Code Division Multiple Access, CDMA without
Line communication technology) it is generally delayed at 3-5 seconds in network.
If a) judging, the time interval between t1 and t2 whether there is the error greater than 5s, if it is determined that between t1 and t2
Time interval be greater than 5s, then t2 is system time, and carries out next step judgement;If it is determined that the time between t1 and t2
Interval is less than or equal to 5s, then t1 is system time, terminates.
If time interval b) between t2 and t3 whether there is the error greater than 5s, if it is determined that between t2 and t3 when
Between interval be greater than 5s, then t3 is system time, and carries out next step judgement;If it is determined that the time interval between t2 and t3
Less than or equal to 5s, then t2 is system time, is terminated.
If time interval c) between t1 and t3 whether there is the error greater than 5s, if it is determined that between t1 and t3 when
Between interval be greater than 5s, then t3 is system time, terminate;If it is determined that the time interval between t1 and t3 is less than or equal to 5s, then
T1 is system time, is terminated.
Embodiment 3
As described in Figure 6, for based on the system in 1 method of embodiment.
A kind of system that multi-scheme obtains network time parallel, comprising:
Parallel requests module, when being obtained for being issued parallel to time synchronization server, hypertext transfer protocol server
Between request and to message queue telemetering transmission server issue subscribe to time theme request;
Module is obtained, is responded for waiting for server, the first network time that the time synchronization server is replied is obtained
And/or the second network time and/or the message queue telemetering transmission service of the hypertext transfer protocol server reply
The third network time of reply;
Computation analysis module, for passing through application condition algorithm from the first network time and/or second network
Time and/or the third network time fall into a trap point counting be precipitated the optimum network time be considered as the grid time.
The technical program is advantageous in that: being passed by time synchronization server described in parallel requests module, the hypertext
Defeated protocol server and described three kinds of servers of message queue telemetering transmission server are simultaneously emitted by the request of acquisition time, obtain
Modulus block receives the response of each server, and computation analysis module is by application condition algorithm from the all-network time received
System time of the optimum network time as internet of things equipment is precipitated in point counting of falling into a trap, and energy effective guarantee is in different operators route
It can succeed and promptly obtain network time.
Embodiment 4
As described in Fig. 6-Fig. 8, for based on embodiment 3, difference from Example 3 is:
The acquisition module includes:
Statistic unit, for counting the number of servers of respond request.
The computation analysis module includes:
Judging unit, for judging whether an only server respond request, if the determination result is YES, then direct handle
The network time that the server is replied is calculated as the grid time;If judging result is no, execution the first calculating analytical unit.
Described first calculates analytical unit, for judging whether only two-server respond request, if judging result is
It is the network time for then comparing two-server reply, the optimum network time is analyzed according to application condition algorithm and is considered as system
Network time;If judging result be it is no, directly execute second calculate analytical unit.
Described first, which calculates analytical unit, includes:
First judgment sub-unit, for judging whether only two-server respond request, if judging result be it is no,
It then directly executes described second and calculates analytical unit.
Whether the second judgment sub-unit, the time interval for judging between two kinds of network times are less than between preset time
Every, if judging result be it is no, choosing a kind of network time replied at first is considered as system time;If the determination result is YES, then
A kind of access time stamp bigger network time is considered as system time.
Described second calculates analytical unit, for the first network time, second network time and described the
Three network times were calculated two-by-two, obtained the first network time, second network time according to application condition algorithm
It analyzes the optimum network time with being calculated among the third network time three and is considered as the grid time.
Described second, which calculates analytical unit, includes:
Comparing subunit, for comparing the first network time, second network time and the third network
Timestamp maximum a kind of network time among time.
Third judgment sub-unit, for choosing any network time and timestamp in addition to timestamp maximum network time
Maximum network time is compared, and judges whether the time interval between two kinds of network times is less than prefixed time interval, if
Judging result be it is no, then choosing a kind of network time first replied is considered as system time;If the determination result is YES, then is executed
Four judgment sub-units.
4th judgment sub-unit, when for choosing a kind of remaining last network time and the maximum network of timestamp
Between be compared, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result is
No, then choosing a kind of network time first replied is considered as system time;If the determination result is YES, then it is single to execute the 5th judgement
Member.
5th judgment sub-unit, for choosing in addition to timestamp maximum network time when remaining two kinds of networks
Between be compared, judge whether the time interval between two kinds of network times is less than prefixed time interval, if judging result is
No, then choosing a kind of network time first replied is considered as system time;If the determination result is YES, then access time stamp teaches big
A kind of network time is considered as system time.
If three servers respond (being followed successively by t1, t2 and t3 according to time order and function), t1 and t2 is first compared, error is in 5s
It is interior, then system time is used as using first reply (being assumed to be t1);It is larger using timestamp in the two if error is greater than 5s
(being assumed to be t2) compares with t3;If error within 5s, uses t2 for system time;If error is greater than 5s, t1 is compared
And t3, if error within 5s, using t1 as system time, if more than 5s, then uses t3 as system time.
Specific embodiment described herein is only an example for the spirit of the invention.Technology belonging to the present invention
The technical staff in field can make various modifications or additions to the described embodiments or using similar method
Substitution, however, it does not deviate from the spirit of the invention or beyond the scope of the appended claims.