Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Ntp client does not work! urgent!

  1. #1
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Question Ntp client does not work! urgent!

    Hi, I am trying to write a ntp client which is going to connect to a time server and get the time..could'nt find qt example although it is possiple to find many c, vc++ or c# clients.. my code does connect but does not get any datagrams, have you any idea? here is my code, i need first to retrieve the time, i did not code rest of it yet.

    Qt Code:
    1. void AtomicClock::on_TimeUpdate_clicked()
    2. {
    3. QHostInfo info = QHostInfo::fromName("0.pool.ntp.org");
    4. QString ipAddress = info.addresses().first().toString();
    5. ui->ipAddress->setText(ipAddress);
    6. udpSocket = new QUdpSocket(this);
    7. udpSocket->connectToHost("0.pool.ntp.org", 123);
    8.  
    9. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    10. connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
    11. }
    12. void AtomicClock::readPendingDatagrams()
    13. {
    14. ui->statusLabel->setText("Reading...");
    15.  
    16. while(udpSocket->hasPendingDatagrams())
    17. {
    18. QByteArray buffer(udpSocket->pendingDatagramSize(), 0);
    19. udpSocket->readDatagram(buffer.data(), buffer.size());
    20. QDataStream stream(buffer);
    21. stream.setVersion(QDataStream::Qt_4_6);
    22. QString newTime;
    23. stream >> newTime;
    24. ui->lineEdit->setText(newTime);
    25. }
    26. }
    27. void AtomicClock::connectSuccsess()
    28. {
    29. ui->time->setText("Connected");
    30. QByteArray timeRequest(48,'0');
    31. QDataStream out(&timeRequest, QIODevice::WriteOnly);
    32. out.setVersion(QDataStream::Qt_4_6);
    33.  
    34. udpSocket->writeDatagram(timeRequest, QHostAddress("0.pool.ntp.org"), 123);
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 31st March 2010 at 23:25.

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Ntp client does not work! urgent!

    Change the order of these instructions:
    Qt Code:
    1. udpSocket->connectToHost("0.pool.ntp.org", 123);
    2.  
    3. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    4. connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
    To copy to clipboard, switch view to plain text mode 

    into this:
    Qt Code:
    1. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    2. connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
    3.  
    4. udpSocket->connectToHost("0.pool.ntp.org", 123);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    tried but still does not work out, connect works with connectToHost but readReady never emits, with use of bind* none of them works neither connect nor readReady..still dont know what is wrong, i need timestamp to build my ntp client further.
    void AtomicClock:n_TimeUpdate_clicked()
    {
    QHostInfo info = QHostInfo::fromName("0.pool.ntp.org");
    QString ipAddress = info.addresses().first().toString();
    ui->ipAddress->setText(ipAddress);
    udpSocket = new QUdpSocket(this);

    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));

    udpSocket->connectToHost("0.pool.ntp.org", 123);
    //udpSocket->bind(QHostAddress(ipAddress),123); *tried that too, to connect to server with bind, because i use writedatagram and readdatagram
    }

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    Firstly, marking your thread as "Urgent" will not get people to reply any quicker to your topic, and in some cases, some people may not reply at all. Remember, we are doing this out of our own free time. We are not being paid.

    Secondly, UDP is a connectionless and lossy protocol. There's no guarantee any of the packets you send out will arrive at there destination, less so if you are going through several routers to the host.

    Some servers may also require authorisation - have you checked the server is accessible with other programs?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Ntp client does not work! urgent!

    Oh... an "urgent" thread, lol

    I have one question for you - why are you using QDataStream? Not that you actually send some data to it but you do realize no NTP server will know how to deserialize data serialized with QDataStream, right?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Ntp client does not work! urgent!

    Just a quick test using netcat against my NTP server shows that the intent of AtomicClock::connectSuccsess(), which is sending a stream of 48 '0' characters to the server (did you mean NUL characters?), does not elicit a response. Perhaps you need to send a well-formed NTP request to the server?

    I would suggest getting a hold of the NTP server code from ntp.org and looking at the source to the bundled ntpdate program.

  7. #7
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    Hello,

    to fatjuicymole, i know that i have just written that after i couldnt make work with this small code about 3 days, its just feeiling and wish..
    second, i have checked the server with windows ntp client and few other free programs too, it works and the server address i use is a free organization server ntp.org. actually i used my code for other servers too so same problem occurs again.

    to wysota, I dindnt think in that way, i try to write just a client program, i need to use QByteArray for time request. It is the same way the other codes do same jobb.. no way tried without datastream, and tried with connectToHost, write, read does not call readPendingDatagrams() anyway.

    to ChrisW67, yeah i ment to send null characters with 48 length.

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Ntp client does not work! urgent!

    Hi,

    Quote Originally Posted by kknd View Post
    Qt Code:
    1. udpSocket->writeDatagram(timeRequest, QHostAddress("0.pool.ntp.org"), 123);
    To copy to clipboard, switch view to plain text mode 
    You can try to force a flush after sending datagram.
    Qt Code:
    1. udpSocket->flush();
    To copy to clipboard, switch view to plain text mode 
    Also use Wireshark sniffer to look if your data is sent and you recieve data from the server.
    Òscar Llarch i Galán

  9. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Ntp client does not work! urgent!

    Quote Originally Posted by kknd View Post
    Hello,
    to ChrisW67, yeah i ment to send null characters with 48 length.
    So 4 zeros or 4 null characters?
    Qt Code:
    1. QChar zeroChar = '0';
    2. QChar nullChar = '\0';
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  10. #10
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    use of flush() makes no difference, i mean char '0', saw that in a c# example..

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Ntp client does not work! urgent!

    Quote Originally Posted by kknd View Post
    use of flush() makes no difference, i mean char '0', saw that in a c# example..
    It would be nice if you actually checked what you were supposed to send instead of randomly trying your luck. Not very efficient...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Ntp client does not work! urgent!

    RFC 1305 Section 3 and Appendix A. The packet format is substantially more complex than an 48-byte block of zero bytes. This is a request and response on my machine.
    Qt Code:
    1. Frame 1 (90 bytes on wire, 90 bytes captured)
    2. Ethernet II, Src: 192.168.1.6 (00:1a:4d:70:61:9c), Dst: CameoCom_c1:3e:76 (00:40:f4:c1:3e:76)
    3. Internet Protocol, Src: 192.168.1.6 (192.168.1.6), Dst: 192.168.1.1 (192.168.1.1)
    4. User Datagram Protocol, Src Port: 53531 (53531), Dst Port: ntp (123)
    5. Network Time Protocol
    6. Flags: 0xe3
    7. 11.. .... = Leap Indicator: alarm condition (clock not synchronized) (3)
    8. ..10 0... = Version number: NTP Version 4 (4)
    9. .... .011 = Mode: client (3)
    10. Peer Clock Stratum: unspecified or unavailable (0)
    11. Peer Polling Interval: 4 (16 sec)
    12. Peer Clock Precision: 0.015625 sec
    13. Root Delay: 1.0000 sec
    14. Root Dispersion: 1.0000 sec
    15. Reference Clock ID: NULL
    16. Reference Clock Update Time: NULL
    17. Originate Time Stamp: NULL
    18. Receive Time Stamp: NULL
    19. Transmit Time Stamp: Apr 2, 2010 02:12:51.5572 UTC
    20.  
    21. 0000 00 40 f4 c1 3e 76 00 1a 4d 70 61 9c 08 00 45 00 .@..>v..Mpa...E.
    22. 0010 00 4c 00 00 40 00 40 11 b7 49 c0 a8 01 06 c0 a8 .L..@.@..I......
    23. 0020 01 01 d1 1b 00 7b 00 38 83 a1 e3 00 04 fa 00 01 .....{.8........
    24. 0030 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 ................
    25. 0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    26. 0050 00 00 cf 5f d1 23 8e a1 83 72 ..._.#...r
    27.  
    28. No. Time Source Destination Protocol Info
    29. 2 0.000254 192.168.1.1 192.168.1.6 NTP NTP server
    30.  
    31. Frame 2 (90 bytes on wire, 90 bytes captured)
    32. Ethernet II, Src: CameoCom_c1:3e:76 (00:40:f4:c1:3e:76), Dst: 192.168.1.6 (00:1a:4d:70:61:9c)
    33. Internet Protocol, Src: 192.168.1.1 (192.168.1.1), Dst: 192.168.1.6 (192.168.1.6)
    34. User Datagram Protocol, Src Port: ntp (123), Dst Port: 53531 (53531)
    35. Network Time Protocol
    36. Flags: 0x24
    37. 00.. .... = Leap Indicator: no warning (0)
    38. ..10 0... = Version number: NTP Version 4 (4)
    39. .... .100 = Mode: server (4)
    40. Peer Clock Stratum: secondary reference (3)
    41. Peer Polling Interval: 4 (16 sec)
    42. Peer Clock Precision: 0.000001 sec
    43. Root Delay: 0.0388 sec
    44. Root Dispersion: 0.1307 sec
    45. Reference Clock ID: 192.231.203.132
    46. Reference Clock Update Time: Apr 2, 2010 02:05:40.4534 UTC
    47. Originate Time Stamp: Apr 2, 2010 02:12:51.5572 UTC
    48. Receive Time Stamp: Apr 2, 2010 02:12:51.5165 UTC
    49. Transmit Time Stamp: Apr 2, 2010 02:12:51.5165 UTC
    50.  
    51. 0000 00 1a 4d 70 61 9c 00 40 f4 c1 3e 76 08 00 45 00 ..Mpa..@..>v..E.
    52. 0010 00 4c 00 00 40 00 40 11 b7 49 c0 a8 01 01 c0 a8 .L..@.@..I......
    53. 0020 01 06 00 7b d1 1b 00 38 82 6e 24 03 04 ec 00 00 ...{...8.n$.....
    54. 0030 09 ef 00 00 21 73 c0 e7 cb 84 cf 5f cf 74 74 10 ....!s....._.tt.
    55. 0040 e8 71 cf 5f d1 23 8e a1 83 72 cf 5f d1 23 84 38 .q._.#...r._.#.8
    56. 0050 8d a6 cf 5f d1 23 84 3b c2 52 ..._.#.;.R
    To copy to clipboard, switch view to plain text mode 

    In the request the first UDP payload byte is the "e3" on line 23 of the listing.

  13. #13
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    thanks to everyone, now it is one step ahead. the code works as a client and retrieves server response. i used wireshark(it didnt like wireless but got the frames with cable connection) to examine the client and server packets, i used my code and an SNTPClient Solution from DaveyM69, the problem was the udp request(as many of you mentioned), i used very stupid(because its not complex) way to code my request. its shown below, now the problem is to get the data(i couldnt read it or show it, dont know why)..

    Qt Code:
    1. void AtomicClock::connectSuccsess()
    2. {
    3. ui->time->setText("Connected");
    4. QByteArray timeRequest(48, 0);
    5. timeRequest[0] = 0x23; //Obs! first byte, important, makes a ntp client version 4, lazy way
    6. //the rest of the btes sends null, it means very wrong timestamps, but i dont need them now..just get the
    7. //time from server.
    8. udpSocket->write(timeRequest);
    9. }
    To copy to clipboard, switch view to plain text mode 

    the second part which is stll not working properly..
    Qt Code:
    1. void AtomicClock::readPendingDatagrams()
    2. {
    3. ui->statusLabel->setText("Reading...");
    4. QByteArray newTime;
    5.  
    6. do {
    7. newTime.resize(udpSocket->pendingDatagramSize());
    8. udpSocket->read(newTime.data(), newTime.size());
    9. } while(udpSocket->hasPendingDatagrams());
    10.  
    11. QString dateTime;
    12. QDataStream in(&newTime, QIODevice::ReadOnly);
    13. in.setVersion(QDataStream::Qt_4_6);
    14. in >> dateTime;
    15. ui->textEdit->setText(dateTime);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 3rd April 2010 at 09:05.

  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Ntp client does not work! urgent!

    You are not getting a response because there's insufficient information in the request you are sending. The minimum that seems to elicit a response from my NTP server is (hex):
    Qt Code:
    1. echo -ne "\xe3\x00\x04\xfa\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | nc -u ptolemy 123 | od -tx1
    2. 0000000 24 03 04 ec 00 00 09 ac 00 00 28 4a c0 e7 cb 84
    3. 0000020 cf 61 03 93 73 5d d6 6e 00 00 00 00 00 00 00 00
    4. 0000040 cf 61 09 af 61 26 62 2c cf 61 09 af 61 29 fa 5e
    To copy to clipboard, switch view to plain text mode 
    The first ten bytes have values. (nc is the netcat utility)

    You still need to disassemble the 64-bit time stamp into something that you can use as a time.

  15. #15
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    Hi, I managed to get response from the ntp server, if you just see my code up there. now the problem is to get the transmit timestamp or read it in a proper way to get the time UTC 1.1.1970 + seconds.. I changed my readPendingDatagrams() function..but it gives ugly reply if i run the code. Can i get little bit help for that? i am just conderned wtih last 8 bytes(transmit stamp)..thanks.
    Qt Code:
    1. void AtomicClock::readPendingDatagrams()
    2. {
    3. ui->statusLabel->setText("Reading...");
    4. QByteArray newTime;
    5. QDateTime Epoch(QDate(1900, 1, 1));
    6. do {
    7. newTime.resize(udpSocket->pendingDatagramSize());
    8. udpSocket->read(newTime.data(), newTime.size());
    9. } while(udpSocket->hasPendingDatagrams());
    10.  
    11. QByteArray TransmitTimeStamp ;
    12. TransmitTimeStamp = newTime.right(8);
    13.  
    14. quint64 seconds = 0;
    15. for (int i=0; i<= 3; i++)
    16. {
    17. seconds = (seconds << 8) | TransmitTimeStamp[i];
    18. }
    19. quint64 fractions = 0;
    20. for (int i=4; i<= 7; i++)
    21. {
    22. fractions = (fractions << 8) | TransmitTimeStamp[i];
    23. }
    24. double ticks = seconds + (fractions / 0x100000000L);
    25.  
    26. ui->textEdit->append(QString::number(ticks,' ', 0));
    27. //QDateTime newestTime = Epoch + ticks;
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    yes, got it! even i cant call that code perfect, it works, sends ntp client request ver. 4 to server on udp port 123 and gets server response 48 bytes, after that TransmitTimeStamp is choosen from the bytearray last (8 bytes), actually i used just the seconds the first 4 bytes but not last 4 bytes(fraction) dont need to be very sensitive within this prog. code includes some labels and texedit objects, the purpose is just to see on gui how the code would run.. i copy my code here, so maybe someone needs in the future. thanks eveyone on this thread..got much help with wireshark, and ntp request advices..

    Qt Code:
    1. #ifndef ATOMICCLOCK_H
    2. #define ATOMICCLOCK_H
    3.  
    4. #include <QMainWindow>
    5. #include <QAbstractSocket>
    6. #include <QUdpSocket>
    7. #include <QHostInfo>
    8. #include <QHostAddress>
    9. #include <QDateTime>
    10. namespace Ui {
    11. class AtomicClock;
    12. }
    13.  
    14. class AtomicClock : public QMainWindow {
    15. Q_OBJECT
    16. public:
    17. AtomicClock(QWidget *parent = 0);
    18. ~AtomicClock();
    19.  
    20. protected:
    21. void changeEvent(QEvent *e);
    22.  
    23. private:
    24. Ui::AtomicClock *ui;
    25. QUdpSocket *udpSocket;
    26.  
    27. private slots:
    28. void on_TimeUpdate_clicked();
    29. void readPendingDatagrams();
    30. void connectSuccsess();
    31. };
    32.  
    33. #endif // ATOMICCLOCK_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "atomicclock.h"
    2. #include "ui_atomicclock.h"
    3.  
    4. AtomicClock::AtomicClock(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::AtomicClock)
    7. {
    8. ui->setupUi(this);
    9. }
    10. AtomicClock::~AtomicClock()
    11. {
    12. delete ui;
    13. udpSocket->close();
    14. }
    15. void AtomicClock::changeEvent(QEvent *e)
    16. {
    17. QMainWindow::changeEvent(e);
    18. switch (e->type()) {
    19. case QEvent::LanguageChange:
    20. ui->retranslateUi(this);
    21. break;
    22. default:
    23. break;
    24. }
    25. }
    26. void AtomicClock::on_TimeUpdate_clicked()
    27. {
    28. QHostInfo info = QHostInfo::fromName("0.pool.ntp.org");
    29. QString ipAddress = info.addresses().first().toString();
    30. ui->ipAddress->setText(ipAddress);
    31. udpSocket = new QUdpSocket(this);
    32.  
    33. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    34. connect(udpSocket, SIGNAL(connected()), this, SLOT(connectSuccsess()));
    35. udpSocket->abort();
    36. udpSocket->connectToHost("0.pool.ntp.org", 123);
    37. }
    38. void AtomicClock::readPendingDatagrams()
    39. {
    40. ui->statusLabel->setText("Reading...");
    41. QByteArray newTime;
    42. QDateTime Epoch(QDate(1900, 1, 1));
    43. QDateTime unixStart(QDate(1970, 1, 1));
    44. do {
    45. newTime.resize(udpSocket->pendingDatagramSize());
    46. udpSocket->read(newTime.data(), newTime.size());
    47. } while(udpSocket->hasPendingDatagrams());
    48.  
    49. QByteArray TransmitTimeStamp ;
    50. TransmitTimeStamp = newTime.right(8);
    51.  
    52. quint64 seconds = 0;
    53. for (int i=0; i<= 3; ++i)
    54. {
    55. seconds = (seconds << 8) | TransmitTimeStamp[i];
    56. }
    57.  
    58. ui->textEdit->append(QString::number(seconds, 10));
    59. QDateTime newestTime;
    60. newestTime.setTime_t(seconds - Epoch.secsTo(unixStart));
    61. ui->textEdit->append(newestTime.toString());
    62. }
    63. void AtomicClock::connectSuccsess()
    64. {
    65. ui->time->setText("Connected");
    66. QByteArray timeRequest(48, 0);
    67. timeRequest[0] = '\x23';
    68. udpSocket->flush();
    69. udpSocket->write(timeRequest);
    70. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    hi again,

    the code over there sometimes does not work, do you know why? i need to make it correct, nedd help of you , guys!

    can not get (or calculate wrong) correct time from server..

    thanks in advance
    Qt Code:
    1. #
    2. void AtomicClock::readPendingDatagrams()
    3. #
    4. {
    5. #
    6. ui->statusLabel->setText("Reading...");
    7. #
    8. QByteArray newTime;
    9. #
    10. QDateTime Epoch(QDate(1900, 1, 1));
    11. #
    12. QDateTime unixStart(QDate(1970, 1, 1));
    13. #
    14. do {
    15. #
    16. newTime.resize(udpSocket->pendingDatagramSize());
    17. #
    18. udpSocket->read(newTime.data(), newTime.size());
    19. #
    20. } while(udpSocket->hasPendingDatagrams());
    21. #
    22.  
    23. #
    24. QByteArray TransmitTimeStamp ;
    25. #
    26. TransmitTimeStamp = newTime.right(8);
    27. #
    28.  
    29. #
    30. quint64 seconds = 0;
    31. #
    32. for (int i=0; i<= 3; ++i)
    33. #
    34. {
    35. #
    36. seconds = (seconds << 8) | TransmitTimeStamp[i];
    37. #
    38. }
    39. #
    40.  
    41. #
    42. ui->textEdit->append(QString::number(seconds, 10));
    43. #
    44. QDateTime newestTime;
    45. #
    46. newestTime.setTime_t(seconds - Epoch.secsTo(unixStart));
    47. #
    48. ui->textEdit->append(newestTime.toString());
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    So what happens instead of what you are expecting?

  19. #19
    Join Date
    Mar 2010
    Posts
    11
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    i am expecting to get transmitt timestamp from server response TransmitTimeStamp = newTime.right(8); last 8 bytes, i use the first 4 bytes to get the date/time, not bothering last 4 bytes (fraction part). og i get often
    seconds 18446744073709551601
    (quint64) Epoch.secsTo(unixStart) 18446744071623573120
    and newestTime.toString() th 7. feb 07:28:01 2036 year 2036, very wrong date

    i just copy the code again, made little bit justify after last try... any idea?
    Qt Code:
    1. void AtomicClock::readPendingDatagrams()
    2. {
    3. QByteArray newTime;
    4. QDateTime Epoch(QDate(1900, 1, 1));
    5. QDateTime unixStart(QDate(1970, 1, 1));
    6. do {
    7. newTime.resize(udpSocket->pendingDatagramSize());
    8. udpSocket->read(newTime.data(), newTime.size());
    9. } while(udpSocket->hasPendingDatagrams());
    10.  
    11. QByteArray TransmitTimeStamp ;
    12. TransmitTimeStamp = newTime.right(8);
    13.  
    14. quint64 seconds = 0;
    15. for (int i=0; i<=3; ++i)
    16. {
    17. seconds = (seconds << 8) | TransmitTimeStamp[i];
    18. }
    19.  
    20. ui->textEdit->append(QString::number(seconds, 10));
    21. QDateTime newestTime;
    22. quint64 diffe = seconds - (quint64) Epoch.secsTo(unixStart);
    23. newestTime.setTime_t(diffe);
    24. ui->textEdit->append(QString::number((quint64) Epoch.secsTo(unixStart), 10));
    25. ui->textEdit->append(newestTime.toString());
    26. }
    To copy to clipboard, switch view to plain text mode 

  20. #20
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ntp client does not work! urgent!

    Since year 2036 is when the date used by SNTP overflows, it would seem that you are receiving an invalid time from the server (possibly 0xFFFFFFFFFFFFFFFF) or your buffering code is incorrect.

    What data are you receiving from the server?

Similar Threads

  1. An Ftp Client to work with web browser
    By thewooferbbk in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2009, 09:04
  2. Urgent! help
    By Sheng in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd October 2008, 21:03
  3. [URGENT] Weird compile error [URGENT]
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 24th May 2008, 23:54
  4. Client/Server doesn't work
    By mattia in forum Newbie
    Replies: 2
    Last Post: 1st November 2007, 13:31
  5. urgent issue qt4.2
    By pratik in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2007, 16:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.