1 | /**
|
---|
2 | * Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
3 | * Licence: BSD
|
---|
4 | * UDP handshake taken from http://www.abc.se/~m6695/udp.html
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "timediff.h"
|
---|
8 |
|
---|
9 | void diep(char *s)
|
---|
10 | {
|
---|
11 | perror(s);
|
---|
12 | exit(1);
|
---|
13 | }
|
---|
14 |
|
---|
15 | int main(int argc, char * argv[]) {
|
---|
16 | printf("Size of .. %li\n", sizeof(uint32_t));
|
---|
17 | printf("Size of .. %li\n", sizeof(struct timeval));
|
---|
18 | struct timeval tv;
|
---|
19 | struct tm *tm;
|
---|
20 |
|
---|
21 | struct sockaddr_in si_me, si_other;
|
---|
22 | int s, i, slen=sizeof(si_other);
|
---|
23 | char buf[BUFLEN];
|
---|
24 |
|
---|
25 | if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
|
---|
26 | diep("socket");
|
---|
27 |
|
---|
28 | memset((char *) &si_me, 0, sizeof(si_me));
|
---|
29 | si_me.sin_family = AF_INET;
|
---|
30 | si_me.sin_port = htons(PORT);
|
---|
31 | si_me.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
32 | if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
|
---|
33 | diep("bind");
|
---|
34 |
|
---|
35 | struct timeval_t packet;
|
---|
36 | for (i=0; i<NPACK; i++) {
|
---|
37 | if (recvfrom(s, &tv, sizeof(tv), 0, (struct sockaddr *)&si_other, &slen)==-1)
|
---|
38 | diep("recvfrom()");
|
---|
39 | gettimeofday(&tv,NULL);
|
---|
40 | packet.tv_sec = tv.tv_sec;
|
---|
41 | packet.tv_usec = tv.tv_usec;
|
---|
42 | if (sendto(s, (const void *)&packet, sizeof(packet), 0, (struct sockaddr *)&si_other, slen)==-1)
|
---|
43 | diep("sendto()");
|
---|
44 | printf("Send %i bytes\n", sizeof(packet));
|
---|
45 | printf("Received packet from %s:%d\n",
|
---|
46 | inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
|
---|
47 | tm=localtime(&tv.tv_sec);
|
---|
48 | printf(" %d:%02d:%02d.%li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
|
---|
49 | }
|
---|
50 |
|
---|
51 | close(s);
|
---|
52 | return 0;
|
---|
53 |
|
---|
54 | exit(0);
|
---|
55 | }
|
---|