Server time C++

Posted on 2003-02-19
Medium Priority
Last Modified: 2012-08-13
i want to get the time and date off the server using C++.

i know that i can get the time on the local machine using Time(), and the date useing Date(). i can even do DateTime();

but what i really want is a bit of code that gets the date and time (seperately) from a server that works with win nt and 2k. :o)

any help would be great.
Question by:lewy
LVL 86

Accepted Solution

jkr earned 400 total points
ID: 7982575
You would use 'NetRemoteTOD()', which returns the time of day information from a specified server. Check out the sample code at http://www.mvps.org/win32/network/nrt.c

#include <stdio.h>
#include <windows.h>
#include <lm.h>
#pragma hdrstop

static const char *dow[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *month[] = { "???", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

int main( int argc, char *argv[] )
     wchar_t server[256] = L"";
     DWORD rc;

     if ( argc > 2 )
          puts( "usage: nrt [servername]" );
          return 1;

     if ( argc > 1 )
          mbstowcs( server, argv[1], strlen( argv[1] ) + 1 );

     rc = NetRemoteTOD( server, (LPBYTE *) &p );
     if ( rc != NERR_Success )
          printf( "error %d\n", rc );
          return 2;

     printf( "Unix time_t:     %lu (since 1/1/70 00:00:00 UTC)\n", p->tod_elapsedt );
     printf( "Milliseconds:    %lu (since boot)\n", p->tod_msecs );
     printf( "Current time:    %02lu:%02lu:%02lu.%02lu (UTC)\n", p->tod_hours, p->tod_mins, p->tod_secs, p->tod_hunds );
     printf( "Timezone offset: %ld (minutes from GMT)\n", p->tod_timezone );
     printf( "Tick length:     %lu (microseconds)\n", p->tod_tinterval * 100 );
     printf( "Current date:    %s, %lu %s %lu\n", dow[p->tod_weekday], p->tod_day, month[p->tod_month], p->tod_year );

     rc = NetApiBufferFree( p ); // no docs -- but I think it's necessary

     return 0;

LVL 12

Expert Comment

ID: 7983091
You could also use the daytime service from standard TCP/IP. Do a 'telnet <host> daytime' from your machine on your command prompt or shell.

<host> is hostname or IP number to the machine.
daytime is the text 'daytime' exactly, 7 letters string.

If you get that daytime is not known try to use the value 13, daytime should be service 13 if I remember correctly.

If you get a daytime then your program can also get that same string:

int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;

struct hostent * he = gethostbyname("host.name.com");

sin.sin_family = AF_INET;
memcpy(& sin.sin_addr, he -> h_addr, sizeof(sin.sin_addr));
sin.sin_port = htons(13); // 13 is daytime
connect(sock,& sin, sizeof(sin));

char buf[100];
int len = recv(sock,buf, sizeof(buf), 0);
printf( "time from server: %s", buf);

If you want to decode the time you can parse the string form daytime it is very easy to parse since every field is at fixed positions in the buffer.

This code should work the same on both un*xes and windows.

If the server doesn't understand TCP version of daytime see if it recognizes UDP version, similar code but specify SOCK_DGRAM instead of SOCK_STREAM when you create the socket. The port number should be the same but the buffer may have different decoding...not sure about that, never used the UDP version for ages myself :-)


Author Comment

ID: 7992467
thanks, i had allready found NetRemoteTOD()
but wasn't sure how to turn the MSecs into a date. good answer, thanks for your help.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question