diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1dc9e55 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ + +all: client server + +client: client.c + gcc -o $@ $@.c -lpthread + +server: server.c + gcc -o $@ $@.c + +clean: + -rm -rf client server diff --git a/client.c b/client.c new file mode 100644 index 0000000..7c67952 --- /dev/null +++ b/client.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFLEN sizeof(struct timeval) +#define PORT 9876 + +static int sock_fd, slen; + +void diep(char * s) +{ + perror(s); + exit(1); +} + +static long long time_diff(struct timeval * one, struct timeval * two) +{ + long long t1 = ((long long) one->tv_sec) * 1000000 + + (long long) one->tv_usec; + long long t2 = ((long long) two->tv_sec) * 1000000 + + (long long) two->tv_usec; + return t1 - t2; +} + +void * thread(void * arg) +{ + struct sockaddr_in si_other; + char time_str[100]; + struct timeval tv_msg, tv_now, tv_last; + for (;;) + { + if (recvfrom(sock_fd, &tv_msg, BUFLEN, 0, + (struct sockaddr *) &si_other, &slen) == -1) + diep("recvfrom()"); + gettimeofday(&tv_now, NULL); + struct tm * lt = localtime(&tv_now.tv_sec); + strftime(time_str, 100, "%F %T", lt); + printf("Rcv: %s, rtt: %lld, elapsed: %lld\n", + time_str, + time_diff(&tv_now, &tv_msg), + time_diff(&tv_now, &tv_last)); + tv_last.tv_sec = tv_now.tv_sec; + tv_last.tv_usec = tv_now.tv_usec; + } +} + +int main(int argc, char * argv[]) +{ + int interval; + struct timeval tv; + struct sockaddr_in si_other; + if (argc != 3) + { + printf("Usage: client server-address msec-interval\n"); + exit(2); + } + interval = atoi(argv[2]) * 1000; + + slen = sizeof(si_other); + + if ((sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) + diep("socket"); + + memset((char *) &si_other, 0, sizeof(si_other)); + si_other.sin_family = AF_INET; + si_other.sin_port = htons(PORT); + inet_aton(argv[1], &si_other.sin_addr); +#if 0 + if (bind(sock_fd, (struct sockaddr *) &si_other, sizeof(si_other)) == -1) + diep("bind"); +#endif + + pthread_t t; + if (pthread_create(&t, NULL, thread, NULL) != 0) + diep("pthread_create()"); + + usleep(100000); + for (;;) + { + gettimeofday(&tv, NULL); + if (sendto(sock_fd, &tv, BUFLEN, 0, + (struct sockaddr *) &si_other, slen) == -1) + diep("sendto()"); + usleep(interval); + } + + close(sock_fd); + return 0; +} + diff --git a/server.c b/server.c new file mode 100644 index 0000000..24da9d1 --- /dev/null +++ b/server.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFLEN sizeof(struct timeval) +#define PORT 9876 + +void diep(char * s) +{ + perror(s); + exit(1); +} + +int main(void) +{ + struct sockaddr_in si_me, si_other; + int s, slen = sizeof(si_other); + struct timeval buf; + + if ((s = 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); + si_me.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind(s, (struct sockaddr *) &si_me, sizeof(si_me)) == -1) + diep("bind"); + + for (;;) + { + if (recvfrom(s, &buf, BUFLEN, 0, + (struct sockaddr *) &si_other, &slen) == -1) + diep("recvfrom()"); + if (sendto(s, &buf, BUFLEN, 0, + (struct sockaddr *) &si_other, slen) == -1) + diep("sendto()"); + } + + close(s); + return 0; +} +