Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. NTP Client library
Qt 6.11 is out! See what's new in the release blog

NTP Client library

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 4.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote on last edited by
    #1

    Can someone suggest me a cross platform (Linux - Windows) library to have the ntp time from a server?
    Regards.

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    K 1 Reply Last reply
    0
    • mrdebugM mrdebug

      Can someone suggest me a cross platform (Linux - Windows) library to have the ntp time from a server?
      Regards.

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @mrdebug

      How about this one?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • mrdebugM Offline
        mrdebugM Offline
        mrdebug
        wrote on last edited by
        #3

        I have bring up this synchronized function that can be used inside a thread.

        // https://github.com/edma2/ntp/blob/master/rfc.txt
        bool GetNTPDateTime(QDateTime &DateTime, QString Server, int Socket) {
            bool Result= false;
            QHostInfo HostInfo= QHostInfo::fromName(Server);
            if (HostInfo.addresses().length()> 0) {
                QUdpSocket *UdpSocket= new QUdpSocket(); {
                    UdpSocket->connectToHost(QHostAddress(HostInfo.addresses().at(0)), Socket);
                    char Message[48]= {010, 0, 0, 0, 0, 0, 0, 0, 0};
                    if (UdpSocket->writeDatagram(Message, sizeof(Message), QHostAddress(HostInfo.addresses().at(0)), Socket)== sizeof(Message)) {
                        if (UdpSocket->waitForReadyRead()) {
                            while (UdpSocket->hasPendingDatagrams()) {
                                QByteArray QBABufferIn= UdpSocket->readAll();
                                if (QBABufferIn.size()== 48) {
                                    int count= 40;
                                    unsigned long DateTimeIn= uchar(QBABufferIn.at(count))+ (uchar(QBABufferIn.at(count+ 1)) << 8)+ (uchar(QBABufferIn.at(count+ 2)) << 16)+ (uchar(QBABufferIn.at(count+ 3)) << 24);
                                    long tmit= ntohl((time_t)DateTimeIn);
                                    tmit-= 2208988800U;
                                    DateTime= QDateTime::fromTime_t(tmit);
                                    Result= true;
                                }
                            }
                        }
                    }
                }{
                    delete UdpSocket;
                }
            }
            return Result;
        }
        

        What about it? I hope to do not have problem with it at the customers.

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        K 1 Reply Last reply
        0
        • mrdebugM mrdebug

          I have bring up this synchronized function that can be used inside a thread.

          // https://github.com/edma2/ntp/blob/master/rfc.txt
          bool GetNTPDateTime(QDateTime &DateTime, QString Server, int Socket) {
              bool Result= false;
              QHostInfo HostInfo= QHostInfo::fromName(Server);
              if (HostInfo.addresses().length()> 0) {
                  QUdpSocket *UdpSocket= new QUdpSocket(); {
                      UdpSocket->connectToHost(QHostAddress(HostInfo.addresses().at(0)), Socket);
                      char Message[48]= {010, 0, 0, 0, 0, 0, 0, 0, 0};
                      if (UdpSocket->writeDatagram(Message, sizeof(Message), QHostAddress(HostInfo.addresses().at(0)), Socket)== sizeof(Message)) {
                          if (UdpSocket->waitForReadyRead()) {
                              while (UdpSocket->hasPendingDatagrams()) {
                                  QByteArray QBABufferIn= UdpSocket->readAll();
                                  if (QBABufferIn.size()== 48) {
                                      int count= 40;
                                      unsigned long DateTimeIn= uchar(QBABufferIn.at(count))+ (uchar(QBABufferIn.at(count+ 1)) << 8)+ (uchar(QBABufferIn.at(count+ 2)) << 16)+ (uchar(QBABufferIn.at(count+ 3)) << 24);
                                      long tmit= ntohl((time_t)DateTimeIn);
                                      tmit-= 2208988800U;
                                      DateTime= QDateTime::fromTime_t(tmit);
                                      Result= true;
                                  }
                              }
                          }
                      }
                  }{
                      delete UdpSocket;
                  }
              }
              return Result;
          }
          

          What about it? I hope to do not have problem with it at the customers.

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @mrdebug

          looks like you have a couple of paranthesis too much (see around delete UdpSocket). If it is sufficient for your purpose it is fine.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved