Created
December 6, 2012 22:39
-
-
Save jon/4229137 to your computer and use it in GitHub Desktop.
Just how many UDP packets *can* I send?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <netinet/in.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/time.h> | |
static double start = 0.0; | |
static long count = 0; | |
static double now() { | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
double ts = tv.tv_sec; | |
ts += tv.tv_usec / 1000000.0; | |
return ts; | |
} | |
void handle_sigint(int sig) { | |
double elapsed = now() - start; | |
double rate = count / elapsed; | |
printf("Sent %lu message in %f seconds for %f msgs/second", count, elapsed, rate); | |
exit(0); | |
} | |
int main(int argc, char **argv) { | |
struct sockaddr_in sin; | |
int s, ret; | |
start = now(); | |
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
if (s < 0) { | |
perror("socket"); | |
exit(-1); | |
} | |
memset(&sin, 0, sizeof(sin)); | |
sin.sin_family = AF_INET; | |
ret = bind(s, (struct sockaddr *)&sin, sizeof(sin)); | |
if (ret < 0) { | |
perror("bind"); | |
exit(-1); | |
} | |
signal(SIGINT, handle_sigint); | |
sin.sin_port = htons(12345); | |
char msg = 'x'; | |
while (1) { | |
ret = sendto(s, &msg, 1, 0, (struct sockaddr *)&sin, sizeof(sin)); | |
if (ret < 0) { | |
perror("sendto"); | |
exit(-1); | |
} | |
count++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment