add option for udp receiving multiple times

This commit is contained in:
HappyZ 2017-02-13 18:03:41 -08:00
parent 12e4a4e24d
commit 5bff1a9955
3 changed files with 63 additions and 48 deletions

View File

@ -41,17 +41,21 @@ int main(int argc, char *argv[])
socklen_t clilen; socklen_t clilen;
// for misc // for misc
int ret; int ret;
unsigned char listenOnce = 0;
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
if (argc < 2) if (argc < 2)
{ {
printf("Usage: %s <port> <[optional] filepath>\n", argv[0]); printf("Usage: %s <port> <[optional] listenOnce ([0]/1)> <[optional] filepath>\n", argv[0]);
exit(0); exit(0);
} }
port = atoi(argv[1]); port = atoi(argv[1]);
if (argc > 2)
listenOnce = atoi(argv[2]);
// listen to socket // listen to socket
listenfd = socket(AF_INET, SOCK_DGRAM, 0); listenfd = socket(AF_INET, SOCK_DGRAM, 0);
// bind socket and listen // bind socket and listen
@ -62,18 +66,24 @@ int main(int argc, char *argv[])
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
// infinity loop to listen // infinity loop to listen
for (;;)
{
printf("Waiting on port %d...\n", port); printf("Waiting on port %d...\n", port);
// clear total_bytes_recv to 0 // clear total_bytes_recv to 0
total_bytes_recv = 0; total_bytes_recv = 0;
// if instrument to write to a file // if instrument to write to a file
if (argc > 2) if (argc > 3)
{ {
fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC); fd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC);
if (fd == -1) { if (fd == -1) {
fprintf(stderr, "! Unable to open file %s.\n", argv[2]); fprintf(stderr, "! Unable to open file %s.\n", argv[3]);
close(listenfd); close(listenfd);
exit(1); if (listenOnce)
break;
continue;
} }
} }
@ -100,9 +110,13 @@ int main(int argc, char *argv[])
// a "code" to indicate UDP is done // a "code" to indicate UDP is done
// printf("%d %d %d", (recvbuf[0] == '='), (recvbuf[1] == '?'), (recvbuf[0] == '!')); // printf("%d %d %d", (recvbuf[0] == '='), (recvbuf[1] == '?'), (recvbuf[0] == '!'));
if ((recvbuf[0] == '=') && (recvbuf[1] == '?') && (recvbuf[2] == '!')) if ((recvbuf[0] == '=') && (recvbuf[1] == '?') && (recvbuf[2] == '!'))
{
if (argc > 3)
close(fd);
break; break;
}
if (argc > 2) if (argc > 3)
write(fd, recvbuf, ret); write(fd, recvbuf, ret);
total_bytes_recv += ret; total_bytes_recv += ret;
@ -110,9 +124,6 @@ int main(int argc, char *argv[])
} }
if (argc > 2)
close(fd);
// end timing // end timing
gettimeofday(&t_end, NULL); gettimeofday(&t_end, NULL);
elapsedTime = (t_end.tv_sec - t_start.tv_sec) + (t_end.tv_usec - t_start.tv_usec) / 1000000.0; elapsedTime = (t_end.tv_sec - t_start.tv_sec) + (t_end.tv_usec - t_start.tv_usec) / 1000000.0;
@ -120,6 +131,10 @@ int main(int argc, char *argv[])
"recv(bytes):%d\nduration(s):%lf\nthroughput(bps):%lf\n", "recv(bytes):%d\nduration(s):%lf\nthroughput(bps):%lf\n",
total_bytes_recv, elapsedTime, total_bytes_recv * 8 / elapsedTime); total_bytes_recv, elapsedTime, total_bytes_recv * 8 / elapsedTime);
if (listenOnce)
break;
}
close(listenfd); close(listenfd);
return 0; return 0;