Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:06:51+00:00 2026-06-13T11:06:51+00:00

Possible Duplicate: Is there any C/C++ library to connect with a remote NTP server?

  • 0

Possible Duplicate:
Is there any C/C++ library to connect with a remote NTP server?

I’m searching for a working code in c/c++ that gets the time and date from the server(ntp.belnet.be). It works with UDP and uses port 123.

Can someone help?

//sending pakket
memset(&sntp_msg_header, 0, sizeof sntp_msg_header);
sntp_msg_header.flags = 27;
sntp_msg_header.originate_timestamp_secs = time(NULL);

// Get data in rxmsg
...
...

// print time
timeval = ntohl(rxmsg.transmit_timestamp_secs) - ((70ul * 365ul + 17ul) * 86400ul);
printf("%s", ctime(&timeval));

This is what i have so far. But i can’t get the correct data out of it.
I hope this is more information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:06:52+00:00Added an answer on June 13, 2026 at 11:06 am

    This is a code snippet in linux. Hope this will help. I don’t know how to convert it to c++.

     * This code will query a ntp server for the local time and display
     * it.  it is intended to show how to use a NTP server as a time
     * source for a simple network connected device.
     * This is the C version.  The orignal was in Perl
     *
     * For better clock management see the offical NTP info at:
     * http://www.eecis.udel.edu/~ntp/
     *
     * written by Tim Hogard (thogard@abnormal.com)
     * Thu Sep 26 13:35:41 EAST 2002
     * Converted to C Fri Feb 21 21:42:49 EAST 2003
     * this code is in the public domain.
     * it can be found here http://www.abnormal.com/~thogard/ntp/
     *
     */
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <time.h>
    #include <string.h>
    
    void ntpdate();
    
    int main() {
        ntpdate();
        return 0;
    }
    
    void ntpdate() {
    char    *hostname="tick.usno.navy.mil";
    int portno=123;     //NTP is port 123
    int maxlen=1024;        //check our buffers
    int i;          // misc var i
    unsigned char msg[48]={010,0,0,0,0,0,0,0,0};    // the packet we send
    unsigned long  buf[maxlen]; // the buffer we get back
    //struct in_addr ipaddr;        //  
    struct protoent *proto;     //
    struct sockaddr_in server_addr;
    int s;  // socket
    int tmit;   // the time -- This is a time_t sort of
    
    //use Socket;
    //
    //#we use the system call to open a UDP socket
    //socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket: $!";
    proto=getprotobyname("udp");
    s=socket(PF_INET, SOCK_DGRAM, proto->p_proto);
    if(s) {
        perror("asd");
        printf("socket=%d\n",s);
    }
    //
    //#convert hostname to ipaddress if needed
    //$ipaddr   = inet_aton($HOSTNAME);
    memset( &server_addr, 0, sizeof( server_addr ));
    server_addr.sin_family=AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(hostname);
    //argv[1] );
    //i   = inet_aton(hostname,&server_addr.sin_addr);
    server_addr.sin_port=htons(portno);
    //printf("ipaddr (in hex): %x\n",server_addr.sin_addr);
    
    /*
     * build a message.  Our message is all zeros except for a one in the
     * protocol version field
     * msg[] in binary is 00 001 000 00000000 
     * it should be a total of 48 bytes long
    */
    
    // send the data
    printf("sending data..\n");
    i=sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
    
    // get the data back
    i=recv(s,buf,sizeof(buf),0);
    printf("recvfr: %d\n",i);
    //perror("recvfr:");
    
    //We get 12 long words back in Network order
    /*
    for(i=0;i<12;i++)
        printf("%d\t%-8x\n",i,ntohl(buf[i]));
    */
    
    /*
     * The high word of transmit time is the 10th word we get back
     * tmit is the time in seconds not accounting for network delays which
     * should be way less than a second if this is a local NTP server
     */
    
    tmit=ntohl((time_t)buf[10]);    //# get transmit time
    //printf("tmit=%d\n",tmit);
    
    /*
     * Convert time to unix standard time NTP is number of seconds since 0000
     * UT on 1 January 1900 unix time is seconds since 0000 UT on 1 January
     * 1970 There has been a trend to add a 2 leap seconds every 3 years.
     * Leap seconds are only an issue the last second of the month in June and
     * December if you don't try to set the clock then it can be ignored but
     * this is importaint to people who coordinate times with GPS clock sources.
     */
    
    tmit-= 2208988800U; 
    //printf("tmit=%d\n",tmit);
    /* use unix library function to show me the local time (it takes care
     * of timezone issues for both north and south of the equator and places
     * that do Summer time/ Daylight savings time.
     */
    
    
    //#compare to system time
    printf("Time: %s",ctime(&tmit));
    i=time(0);
    //printf("%d-%d=%d\n",i,tmit,i-tmit);
    printf("System time is %d seconds off\n",i-tmit);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there any working memory profiler for Python3 I have some script
Possible Duplicate: Concatenate Two NSDate String values Just confused that is there any default
Possible Duplicate: Cross Platform C library for GUI Apps? Is there any crossplatform GUI
Possible Duplicate: Are there any free cmd-line scripts which can re-format PHP source-code? I've
Possible Duplicate: How to reverse an audio file? Is there any way/library in objective-c
Possible Duplicate: Good STL-like library for C Are there any open source C libraries
Possible Duplicate: Is there any NoSQL that is ACID compliant? So, I heard NoSQL
Possible Duplicate: Detecting endianness programmatically in a C++ program Is there any library function
Possible Duplicate: are there any O(1/n) algorithms? Is it ever possible for your code
Possible Duplicate: Good STL-like library for C Are there any open source C libraries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.