source: timediff/timediff.c@ 359

Last change on this file since 359 was 116, checked in by Rick van der Zwet, 15 years ago

Timediff hack

File size: 3.6 KB
Line 
1/**
2 * Rick van der Zwet <info@rickvanderzwet.nl>
3 * Licence: BSD
4 */
5
6#include "timediff.h"
7#include <errno.h>
8
9void diep(char *s)
10{
11 perror(s);
12 exit(1);
13}
14
15int main(int argc, char * argv[]) {
16 printf("Size of .. : %li\n", sizeof(uint32_t));
17 printf("Size of .. : %li\n", sizeof(struct timeval_t));
18 printf("Size of .. : %li\n", sizeof(struct timeval));
19 struct timeval tv, tv2, tv3, tv4, tv_delta, tv_delta_system;
20 struct tm *tm, *tm2;
21 struct timeval_t packet;
22 gettimeofday(&tv, NULL);
23 tm=localtime(&tv.tv_sec);
24 printf("Started at: %d:%02d:%02d.%li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
25
26 struct sockaddr_in si_other, si_me;
27 int s, r, i, slen=sizeof(si_other);
28 char buf[BUFLEN];
29
30 /* Set up receiving socket */
31 if ((r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
32 diep("socket");
33
34 memset((char *) &si_me, 0, sizeof(si_me));
35 si_me.sin_family = AF_INET;
36 si_me.sin_port = htons(PORT_CLIENT);
37 si_me.sin_addr.s_addr = htonl(INADDR_ANY);
38 if (bind(r, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
39 diep("bind");
40
41 if ((r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
42 diep("socket");
43
44 /* Prepare server connection */
45 memset((char *) &si_other, 0, sizeof(si_other));
46 si_other.sin_family = AF_INET;
47 si_other.sin_port = htons(PORT);
48 if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
49 fprintf(stderr, "inet_aton() failed\n");
50 exit(1);
51 }
52
53 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
54 diep("socket");
55
56 /* We don't care in slow packets anymore */
57 struct timeval tv_timeout;
58 tv_timeout.tv_sec = 1L;
59 tv_timeout.tv_usec = 0L;
60 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) != 0)
61 diep("setsockopt");
62
63
64 for (i=0; i<NPACK; i++) {
65 printf("Sending packet %d and waiting for response\n", i);
66 gettimeofday(&tv, NULL);
67 packet.tv_sec = tv.tv_sec;
68 packet.tv_usec = tv.tv_usec;
69 if (sendto(s, (const void *)&packet, sizeof(packet), 0, (struct sockaddr *)&si_other, slen)==-1)
70 diep("sendto()");
71
72 /* This will be generated more and less the same time as on the target side */
73 gettimeofday(&tv3, NULL);
74
75 packet.tv_usec = 0;
76 packet.tv_sec = 0;
77
78 if (recv(s, &packet, sizeof(packet), 0)==-1) {
79 if (errno == EAGAIN) {
80 perror("ERROR: No response received in time");
81 continue;
82 } else {
83 diep("recvfrom()");
84 }
85 }
86 gettimeofday(&tv4, NULL);
87 tv2.tv_sec = packet.tv_sec;
88 tv2.tv_usec = packet.tv_usec;
89
90 printf("Received %li bytes\n", sizeof(packet));
91
92 /* Display and calculations */
93 tm=localtime(&tv.tv_sec);
94 tm2=localtime(&tv2.tv_sec);
95 printf("Send timestamp: %d:%02d:%02d.%06li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
96 printf("Received timestamp: %d:%02d:%02d.%06li \n", tm2->tm_hour, tm2->tm_min, tm2->tm_sec, tv2.tv_usec);
97
98 tv_delta.tv_sec = tv4.tv_sec - tv.tv_sec;
99 tv_delta.tv_usec = tv4.tv_usec - tv.tv_usec;
100 printf("Time delta at local system (including transmit/receive): %02d.%06li\n",
101 (int)(tv_delta.tv_sec), tv_delta.tv_usec);
102
103 tv_delta_system.tv_sec = (tv2.tv_sec - tv.tv_sec) - tv_delta.tv_sec / 2;
104 tv_delta_system.tv_usec = (tv2.tv_usec - tv.tv_usec) - tv_delta.tv_usec / 2;
105 printf("Time delta between sender and target: %02d.%06li\n",
106 (int)(tv_delta_system.tv_sec), tv_delta_system.tv_usec);
107 printf("\n");
108 }
109
110 close(s);
111 return 0;
112
113 exit(0);
114}
Note: See TracBrowser for help on using the repository browser.