/** * Rick van der Zwet * Licence: BSD */ #include "timediff.h" #include void diep(char *s) { perror(s); exit(1); } int main(int argc, char * argv[]) { printf("Size of .. : %li\n", sizeof(uint32_t)); printf("Size of .. : %li\n", sizeof(struct timeval_t)); printf("Size of .. : %li\n", sizeof(struct timeval)); struct timeval tv, tv2, tv3, tv4, tv_delta, tv_delta_system; struct tm *tm, *tm2; struct timeval_t packet; gettimeofday(&tv, NULL); tm=localtime(&tv.tv_sec); printf("Started at: %d:%02d:%02d.%li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec); struct sockaddr_in si_other, si_me; int s, r, i, slen=sizeof(si_other); char buf[BUFLEN]; /* Set up receiving socket */ if ((r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT_CLIENT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(r, (struct sockaddr *)&si_me, sizeof(si_me))==-1) diep("bind"); if ((r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); /* Prepare server connection */ memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if (inet_aton(SRV_IP, &si_other.sin_addr)==0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); /* We don't care in slow packets anymore */ struct timeval tv_timeout; tv_timeout.tv_sec = 1L; tv_timeout.tv_usec = 0L; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) != 0) diep("setsockopt"); for (i=0; itm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec); printf("Received timestamp: %d:%02d:%02d.%06li \n", tm2->tm_hour, tm2->tm_min, tm2->tm_sec, tv2.tv_usec); tv_delta.tv_sec = tv4.tv_sec - tv.tv_sec; tv_delta.tv_usec = tv4.tv_usec - tv.tv_usec; printf("Time delta at local system (including transmit/receive): %02d.%06li\n", (int)(tv_delta.tv_sec), tv_delta.tv_usec); tv_delta_system.tv_sec = (tv2.tv_sec - tv.tv_sec) - tv_delta.tv_sec / 2; tv_delta_system.tv_usec = (tv2.tv_usec - tv.tv_usec) - tv_delta.tv_usec / 2; printf("Time delta between sender and target: %02d.%06li\n", (int)(tv_delta_system.tv_sec), tv_delta_system.tv_usec); printf("\n"); } close(s); return 0; exit(0); }