How do I receive data from NTP server?

前端 未结 2 822
迷失自我
迷失自我 2020-12-22 14:58

I have no idea why send data is 48 bytes 010,0,0..., someone can explain? the problem is buffer for data received, I don\'t know how big he should be, and even if I receive

相关标签:
2条回答
  • 2020-12-22 15:33

    Ok it works, main part of code:

            const char msg[48] = { 010,0,0,0,0,0,0,0,0 };
    
            if (send(hSocket, msg, sizeof(msg) , 0) == SOCKET_ERROR)
                throw HRException("failed to send data.");
    
    
    
            cout << "request sent.\n";
    
            cout << "Dumping received data...\n\n";
    
    
            char   tempBuffer[1024];
            int bytes =  recv(hSocket, tempBuffer, sizeof(tempBuffer), 0);  
            cout << "bytes received: " << bytes << endl;
    
            time_t tmit;
            tmit = ntohl(((time_t*)tempBuffer)[4]);
            tmit -= 2208988800U;
    
            cout << ctime(&tmit);
    

    No idea why data that we send is

    msg[48] = { 010,0,0,0,0,0,0,0,0 };
    

    and why received data contains many numbers? for example if change code to

    tmit = ntohl(((time_t*)tempBuffer)[6]);

    I will get date 2008y, why?

    Guys why so many minuses?, still waiting for an explanation :D

    Here's whole code http://pastebin.com/Sv3ERGfV , dont forget to link ws2_32.lib

    0 讨论(0)
  • 2020-12-22 15:42

    Similar to my issue when trying to query the time from a self-hostet Windows-NTP-Server with the C++ library NTPClient which uses boost for the network tasks, msg[48] = { 010,0,0,0,0,0,0,0,0 }; configures the ntp.flags.mode. After comparing the network traffic of w32tm /stripchart /computer:10.159.96.65 using Wireshark, flag 27 or 11 seem to be the choices for my usecase: Comparison of NTP network packages

    tmit = ntohl(((time_t*)tempBuffer)[6]); extracts the data from the received package. It looks like

    • 4 yields the reference time (last sync with timeserver I assume),
    • 8 the time when server received request and
    • 10 the transmit time (which should be almost equal).
    0 讨论(0)
提交回复
热议问题