Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Sync

Status
Not open for further replies.

flomo

Programmer
Jul 25, 2006
1
US
I am trying to synchronize the clock time on a Windows XP client with a the time on a Linux Server with no success. Below is snippet of the C++ code I am using. Cany anyone please help? (SOS!!)

//This Program is to synchronize time between a Linux server and a Windows XP client

#include <sys/types.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <time.h>

#define SA struct sockaddr *

int main (int argc, char *argv[])
{
struct sockaddr_in serv;
struct hostent *phe;
char request[400],reply[400],tmp[5];
int sockfd, n, err;
IN_ADDR host_ip;
char *temp;
SYSTEMTIME *my_sys_t;

WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD(1, 1);

err = WSAStartup(wVersionRequested, &wsaData);

if (err != 0)
return 1;

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {

WSACleanup();
return 2;
}

if (argc != 2) {
printf("Requested name of timeserver");
return 3;
}

temp = argv[1];

if (isalpha(temp[0])) {
printf("%s \n", temp);
phe = gethostbyname(temp);
if (!phe) {
printf("name not listed in hosts file\n");
return 4;
}
host_ip = *(IN_ADDR*)phe->h_addr;
}
else {
host_ip.s_addr = inet_addr(temp);
}

memset(&serv,0,sizeof(serv));
serv.sin_addr.s_addr = host_ip.s_addr;

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf ("socket error: ");
printf ("%d \n", sockfd);
return 5;
}

serv.sin_family = AF_INET;

serv.sin_port = htons(13);

if (sendto(sockfd, request, 400, 0,
(SA) &serv, sizeof(serv)) != 400) {
printf("sendtorecv error");
err = WSAGetLastError();
printf("%d \n",err);
return 6;
}

if ((n = recvfrom(sockfd, reply, 400, 0,
(SA) NULL, (int *) NULL)) < 0) {
printf("recv error");
err = WSAGetLastError();
printf("%d \n",err);
return 7;
}

// GetLoacalTime(my_sys_t);
_timezone;

strset(tmp, '\0');
strncpy(tmp,reply,3);
_strupr(tmp); ...................(more)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top