In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to achieve NTP server time synchronization with Qt". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Qt how to achieve NTP server time synchronization" bar!
I. Preface
A lot of software has the function of time synchronization, especially Qt on embedded devices, sometimes there are many programs without UI interface, and there is a clock on the hardware, it is inevitable that there will be no electricity for a long time, and it is necessary to synchronize time from the server to ensure that the local time is correct, otherwise the time of some local logs recorded are incorrect, many of which may still be in 1970. NTP synchronization time is a standard protocol, the port used is 123port, this port is very powerful, unexpectedly occupy 123this port, bunker! Using NTP service to synchronize time, you need to set up a time server IP address, which can be found on the Internet. Sometimes, Microsoft's own line does not work, because the default UDP protocol is unreliable, so it is possible to lose packets. It is recommended to choose some domestic time servers, such as some university time servers, which are more accurate and reliable.
2. NtpClient::NtpClient (QObject * parent): QObject (parent) {ntpIP = "202.120.2.101"; udpSocket = new QUdpSocket (this); connect (udpSocket, SIGNAL (connected ()), this, SLOT (sendData (); connect (udpSocket, SIGNAL (readyRead ()), this, SLOT (readData ();} void NtpClient::sendData () {qint8 LI = 0; qint8 VN = 3; qint8 MODE = 3; qint8 STRATUM = 0 Qint8 POLL = 4; qint8 PREC =-6; QDateTime epoch (QDate (1900, 1,1)); qint32 second = quint32 (epoch.secsTo (QDateTime::currentDateTime (); qint32 temp = 0; QByteArray timeRequest (48,0); timeRequest [0] = (LI 24); temp = 0; timeRequest [41] = (temp = (second & 0x00ff0000) > 16); temp = 0; timeRequest [42] = (temp = (second & 0x0000ff00) > > 8) Temp = 0; timeRequest [43] = ((second & 0x000000ff)); udpSocket- > write (timeRequest);} void NtpClient::readData () {QByteArray newTime; QDateTime epoch (QDate (1900, 1,1)); QDateTime unixStart (QDate (1970, 1,1)); while (udpSocket- > hasPendingDatagrams ()) {newTime.resize (udpSocket- > pendingDatagramSize ()); udpSocket- > read (newTime.data (), newTime.size ());}; QByteArray transmitTimeSt TransmitTimeStamp = newTime.right (8); quint32 seconds = transmitTimeStamp.at (0); quint8 temp = 0; for (int I = 1; I ntpIP! = ntpIP) {this- > ntpIP = ntpIP;}} void NtpClient::getDateTime () {udpSocket- > abort (); udpSocket- > connectToHost (ntpIP, 123);} III.
At this point, I believe you have a deeper understanding of "Qt how to achieve NTP server time synchronization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.