///////////////////////////////////////// // simple unix client to be used in // comparing client/server clock readings // // main algorithm: // James Maxlow // // generic socket processes (modified): // RPC Programming Nutshell ///////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include ////////////////////// /* server dependent */ ////////////////////// #define PORT 9090 #define HOST "defender" int main() { ///////////////////////////////////////////// /* used for calculating propagation delays */ ///////////////////////////////////////////// double propdelays[10]; double sum; double avgpropdelay; double maxpropdelay; double minpropdelay; int propdelaycount = 50; ////////////////////////////////////// /* client and server time variables */ ////////////////////////////////////// hrtime_t going, coming; /* high-res nanseconds for measuring prop delay */ timeval clienttime; /* client's time struct in seconds and microseconds */ int dst = 3600; /* set to 3600 or 0 for d.s.t. */ int timezonediff = -18000; /* adjust for local timezone */ int serverdayportion = 0; /* server seconds elapsed in current day */ int clientdayportion = 0; /* client seconds elapsed in current day */ int clienttimehours = 0; /* client time */ int clienttimeminutes = 0; /* client time */ int clienttimeseconds = 0; /* client time */ int clienttimemilliseconds = 0; /* client time */ int settimetohours = 0; /* new client time */ int settimetominutes = 0; /* new client time */ int settimetoseconds = 0; /* new client time */ int settimetomilliseconds = 0; /* new client time */ //////////////////////////////////////////////////////// /* used to hold server times as strings and long ints */ //////////////////////////////////////////////////////// char messagereply[150]; char serversecstring[32]; char serverusecstring[32]; long serverseclong; long serveruseclong; int i = 0; int j, k; ofstream outFile("TIMELOG", ios::app); ////////////////////// /* socket variables */ ////////////////////// struct sockaddr_in sin; struct sockaddr_in pin; struct hostent *hp; char hostname[20]; int sd; //////////////////////////////////////////////////////////// /* get client time and parse (only for printout purposes) */ //////////////////////////////////////////////////////////// gettimeofday(&clienttime,0); clientdayportion = clienttime.tv_sec%86400 + timezonediff + dst; if (clientdayportion < 0) clientdayportion = clientdayportion + 86400; /* accout for early morning GMT */ clienttimehours = clientdayportion/3600; clienttimeminutes = (clientdayportion%3600)/60; clienttimeseconds = clientdayportion%60; clienttimemilliseconds = clienttime.tv_usec/1000; ////////////////////////////// /* generic socket processes */ ////////////////////////////// strcpy(hostname,HOST); /* go find out about the desired host machine */ if ((hp = gethostbyname(hostname)) == 0) { perror("gethostbyname"); exit(1); } /* fill in the socket structure with host information */ memset(&pin, 0, sizeof(pin)); pin.sin_family = AF_INET; pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; pin.sin_port = htons(PORT); /* grab an internet domain socket */ if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } /* connect to PORT on HOST */ if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) { perror("connect"); exit(1); } /////////////////////////////////////// /* send message loop for prop delays */ /////////////////////////////////////// for (i=0; i maxpropdelay) maxpropdelay = propdelays[i]; else if (propdelays[i] < minpropdelay) minpropdelay = propdelays[i]; } avgpropdelay = (sum-maxpropdelay-minpropdelay)/double(propdelaycount-2); /* throw out highest and lowest */ ///////////////////////////////////////////////////////// /* now ask server for time in seconds and microseconds */ ///////////////////////////////////////////////////////// /* send junk message to the server PORT on machine HOST */ if (send(sd, "123", 3, 0) == -1) { perror("send"); exit(1); } /* wait for a message to come back from the server */ if (recv(sd, messagereply, 70, 0) == -1) { perror("recv"); exit(1); } ///////////////////////////// /* parse server time reply */ ///////////////////////////// j = 0; while (messagereply[j] != ' ') { serversecstring[j]=messagereply[j]; j++; } // end while serversecstring[j]='\0'; j = j+3; k = 0; while (messagereply[j] !='\0') { serverusecstring[k]=messagereply[j]; k++; j++; } // end while serverusecstring[k]='\0'; ////////////////////////////////////////////////////////// /* change server time strings to long ints and break up */ ////////////////////////////////////////////////////////// serverseclong = atol(serversecstring); serveruseclong = atol(serverusecstring); serverdayportion = serverseclong%86400 + timezonediff + dst; if (serverdayportion < 0) serverdayportion = serverdayportion + 86400; /* accout for early morning GMT */ settimetohours = serverdayportion/3600; settimetominutes = (serverdayportion%3600)/60; settimetoseconds = serverdayportion%60; settimetomilliseconds = serveruseclong/1000.0 + avgpropdelay/1000000.0; /////////////////////// /* show user summary */ /////////////////////// cout << "Set clock to " << settimetohours << " hours, " << settimetominutes << " minutes, " << settimetoseconds << " seconds, and " << settimetomilliseconds << " milliseconds." << endl; /////////////////////////////////////////////////// /* write two times to file, including prop delay */ /////////////////////////////////////////////////// outFile << "Server time, adjusted with prop delay: " << settimetohours << " hrs, " << settimetominutes << " min, " << settimetoseconds << " sec, and " << settimetomilliseconds << " millisec." << endl; outFile << "Client time: " << clienttimehours << " hrs, " << clienttimeminutes << " min, " << clienttimeseconds << " sec, and " << clienttimemilliseconds << " millisec." << endl; shutdown(sd, 2); return 0; }