?
Solved

Server time C++

Posted on 2003-02-19
3
Medium Priority
?
452 Views
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.
0
Comment
Question by:lewy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
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"";
     TIME_OF_DAY_INFO *p = NULL;
     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;
}

0
 
LVL 12

Expert Comment

by:Salte
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 :-)

Alf
0
 

Author Comment

by:lewy
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

762 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