int setTimeBasicMode () { // Getting time from server // =========================================== struct tm * time_server; time_server = (struct tm *) malloc(sizeof(struct tm)); time_server = getTimeFromServer(); // Set time on local machine // =========================================== setTimeOnMachine(time_server); // Console output: // Server Timestamp // =========================================== printf("%s", asctime( time_server ) ); }
int main(int argc, char *argv[]) //1, Server IP; { int sockfd; struct sockaddr_in serverAddr; if(argc != 2)//TODO: need modify; { printf("Need args: Server_IP"); return -1; } if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("Error: Fail to create socket. ERRNO: %d\n", errno); return -1; } //Set server ip & port //bzero(&serverAddr, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr(argv[1]); serverAddr.sin_port = htons(2100); bzero(&(serverAddr.sin_zero), 8); //msgq int msgId = msgget(MSGKEY, IPC_CREAT | 0666); if(msgId < 0) { printf("ERROR: Cannot create the message queue. ERRNO: %d.\n", errno); return -1; } int selectOption = 0; printMenu(); scanf("%d", &selectOption); while(selectOption != 7) { switch(selectOption) { case 1: connectToServer(sockfd, &serverAddr); break; case 2: disconnectFromServer(sockfd, serverAddr); break; case 3: getTimeFromServer(sockfd, serverAddr); //printMsg(msgId); break; case 4: getNameFromServer(sockfd, serverAddr); //printMsg(msgId); break; case 5: getClientsFromServer(sockfd, serverAddr); //printMsg(msgId); break; case 6: sendMsgToAnotherClient(sockfd, serverAddr); //printMsg(msgId); break; } scanf("%d", &selectOption); } if(currentStatus) disconnectFromServer(sockfd, serverAddr); printf("Bye!\n"); return 0; }
int setTimeChristianMode () { time_t rawtime; int sec_before, sec_after; // Getting local time before request // =========================================== struct tm * time_before_request; time ( &rawtime ); time_before_request = localtime ( &rawtime ); sec_before = time_before_request->tm_sec; // usleep(6500000); // DEBUG 1,5 second // Getting time from server // =========================================== struct tm * time_server; time_server = (struct tm *) malloc(sizeof(struct tm)); time_server = getTimeFromServer(); // Getting local time after request // =========================================== struct tm * time_after_request; time ( &rawtime ); time_after_request = localtime ( &rawtime ); sec_after = time_after_request->tm_sec; // Check if it's same minute before and // after request. Try again if is equals. // =========================================== if (time_before_request->tm_min != time_after_request->tm_min) { setTimeChristianMode (); exit(0); } // Chistian Algorithm // =========================================== // Check drift rate in seconds float drift_rate = ( (float)(sec_after - sec_before) )/2; // Set time with drift rate fix time_server->tm_sec = time_server->tm_sec + drift_rate; // Check if seconds is higher than 59 // Try again if is. // =========================================== if (time_server->tm_sec > 59) { setTimeChristianMode (); exit(0); } // Set time on local machine // =========================================== setTimeOnMachine(time_server); // Console output: // Server Timestamp // =========================================== printf("%s", asctime( time_server ) ); // DEBUG // printf("Second before : %d\n", sec_before); // printf("Second after : %d\n", sec_after); // printf("drift_rate float : %f\n", drift_rate); // printf("drift_rate int : %d\n", (int)drift_rate); }