/******************************************************************************\
* ntp.c - Simple UDP client using Winsock 1.1
*
* Get time from internet by NTP protocol
\******************************************************************************/
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NTP_PORT (unsigned short)123
#define NTP_PROTO SOCK_DGRAM // UDP
#define NTP_DATA_LEN 48
#define TIMEZONE 8
#define MAXDAYINYEAR 366
#define MINDAYINYEAR 365
#define MAXDAYINMONTH 31
#define GENDAYINMONTH 30
#define SPEDAYINMONTH 29
#define MINDAYINMONTH 28
char sNowTime[20];
struct time_s
{
unsigned int t_year;
unsigned int t_month;
unsigned int t_day;
unsigned int t_hour;
unsigned int t_min;
unsigned int t_sec;
unsigned int t_weekday;
};
struct time_s gettimefromsec(unsigned long allsec);
char *ClockTime(struct time_s RealTime);
void main(int argc, char **argv)
{
struct time_s RealTime;
char server_name[100];
unsigned int addr;
char data[NTP_DATA_LEN];
unsigned long allsec;
unsigned long oldsec;
unsigned short port = NTP_PORT;
int retval;
int socket_type = NTP_PROTO;
struct sockaddr_in server;
struct hostent *hp;
WSADATA wsaData;
SOCKET conn_socket;
SYSTEMTIME tm;
if (argc >1)
strcpy(server_name,argv[1]);
else
strcpy(server_name,"time.nist.gov");
if ((retval = WSAStartup(0x202,&wsaData)) != 0) {
fprintf(stderr,"WSAStartup failed with error %d\n",retval);
WSACleanup();
exit(1);
}
memset(&RealTime,0,sizeof(struct time_s));
//
// Attempt to detect if we should call gethostbyname() or
// gethostbyaddr()
if (isalpha(server_name[0])) { /* server address is a name */
hp = gethostbyname(server_name);
}
else { /* Convert nnn.nnn address to a usable one */
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr,4,AF_INET);
}
if (hp == NULL ) {
fprintf(stderr,"Client: Cannot resolve address: Error %d\n",
WSAGetLastError());
WSACleanup();
exit(1);
}
//
// Copy the resolved information into the sockaddr_in structure
//
memset(&server,0,sizeof(server));
memcpy(&(server.sin_addr),hp->h_addr,hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET,socket_type,0); /* Open a socket */
if (conn_socket <0 ) {
fprintf(stderr,"Client: Error Opening socket: Error %d\n",
WSAGetLastError());
WSACleanup();
exit(1);
}
printf("Send time request to: %s\n",hp->h_name);
if (connect(conn_socket,(struct sockaddr*)&server,sizeof(server))
== SOCKET_ERROR) {
fprintf(stderr,"connect() failed: %d\n",WSAGetLastError());
WSACleanup();
exit(1);
}
// send time request
memset(data,0,NTP_DATA_LEN);
memset(data,0x13,1);
retval = send(conn_socket,data,NTP_DATA_LEN,0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"send() failed: error %d\n",WSAGetLastError());
WSACleanup();
exit(1);
}
retval = recv(conn_socket,data,NTP_DATA_LEN,0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError());
closesocket(conn_socket);
WSACleanup();
exit(1);
}
//
// We are not likely to see this with UDP, since there is no
// 'connection' established.
//
if (retval == 0)
{
printf("Server closed connection\n");
closesocket(conn_socket);
WSACleanup();
exit(1);
}
oldsec = ntohl(*(unsigned long *)(data+NTP_DATA_LEN-8));
printf("Received %d bytes, data [%lx] from server\n",retval,oldsec);
// send time request
memset(data,0,NTP_DATA_LEN);
memset(data,0x13,1);
retval = send(conn_socket,data,NTP_DATA_LEN,0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"send() failed: error %d\n",WSAGetLastError());
WSACleanup();
exit(1);
}
retval = recv(conn_socket,data,NTP_DATA_LEN,0 );
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"recv() failed: error %d\n",WSAGetLastError());
closesocket(conn_socket);
WSACleanup();
exit(1);
}
//
// We are not likely to see this with UDP, since there is no
// 'connection' established.
//
if (retval == 0)
{
printf("Server closed connection\n");
closesocket(conn_socket);
WSACleanup();
exit(1);
}
allsec = ntohl(*(unsigned long *)(data+NTP_DATA_LEN-8));
allsec += (allsec-oldsec)/2;
printf("Received %d bytes, data [%lx] from server\n",retval,allsec);
closesocket(conn_socket);
WSACleanup();
RealTime = gettimefromsec(allsec);
tm.wYear = RealTime.t_year;
tm.wMonth = RealTime.t_month;
tm.wDay = RealTime.t_day;
tm.wHour = RealTime.t_hour;
tm.wMinute = RealTime.t_min;
tm.wSecond = RealTime.t_sec;
tm.wMilliseconds = 0;
SetSystemTime(&tm);
allsec += TIMEZONE*60*60;
RealTime = gettimefromsec(allsec);
printf("\nNow time is: %s, today is %d\n",ClockTime(RealTime),RealTime.t_weekday);
exit(0);
}
struct time_s gettimefromsec(unsigned long real_sec)
{
struct time_s nettime;
unsigned long real_day;
long tmp_day;
unsigned int year;
unsigned int month;
unsigned int day;
unsigned int weekday;
unsigned int hour;
unsigned int minute;
unsigned int second;
real_day = ((real_sec/60)/60)/24;
weekday = real_day%7+1;
tmp_day = real_day;
year = 1900;
while(1)
{
if(year%400==0 || (year%4==0 && year%100!=0))
tmp_day -= MAXDAYINYEAR;
else
tmp_day -= MINDAYINYEAR;
if(tmp_day < 0)
break;
year++;
real_day = tmp_day;
}
tmp_day = real_day;
month = 1;
while(1)
{
if(month%2)
{
if(month<=7)
tmp_day -= MAXDAYINMONTH;
else
tmp_day -= GENDAYINMONTH;
}
else
{
if(month<=6)
{
if(month==2)
{
if(year%400==0 || (year%4==0 && year%100!=0))
tmp_day -= SPEDAYINMONTH;
else
tmp_day -= MINDAYINMONTH;
}
else
tmp_day -= GENDAYINMONTH;
}
else
tmp_day -= MAXDAYINMONTH;
}
if(tmp_day < 0)
break;
month++;
real_day = tmp_day;
}
day = real_day+1;
hour = ((real_sec/60)/60)%24;
minute = (unsigned int)((real_sec/60)%60);
second = (unsigned int)(real_sec%60);
nettime.t_year = year;
nettime.t_month = month;
nettime.t_day = day;
nettime.t_weekday = weekday;
nettime.t_hour = hour;
nettime.t_min = minute;
nettime.t_sec = second;
return nettime;
}
char *ClockTime(struct time_s RealTime)
{
CHAR month[3],day[3],hour[3],min[3],sec[3];
if(RealTime.t_year == 0)
{
sprintf(sNowTime,"1900-01-01 00:00:00");
}
else
{
month[0] = RealTime.t_month/10 + 48;
month[1] = RealTime.t_month%10 + 48;
month[2] = '\0';
day[0] = RealTime.t_day/10 + 48;
day[1] = RealTime.t_day%10 + 48;
day[2] = '\0';
hour[0] = RealTime.t_hour/10 + 48;
hour[1] = RealTime.t_hour%10 + 48;
hour[2] = '\0';
min[0] = RealTime.t_min/