Initial commit of sender.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 28 May 2012 11:42:59 +0000 (13:42 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 28 May 2012 11:42:59 +0000 (13:42 +0200)
sender.c [new file with mode: 0644]

diff --git a/sender.c b/sender.c
new file mode 100644 (file)
index 0000000..826bb1d
--- /dev/null
+++ b/sender.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <time.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define PACKET_SIZE 1200
+
+static int burst_sizes[] = { 1, 2, 3, 5, 10, 20, 50, 100 };
+static int num_burst_sizes = sizeof(burst_sizes) / sizeof(burst_sizes[0]);
+
+static void send_burst(int sock, const struct sockaddr *addr, socklen_t addr_len, int burst_size)
+{
+       static char buf[PACKET_SIZE] = { 0 };
+       static int pkt_num = 0;
+       int i;
+
+       for (i = 0; i < burst_size; ++i) {
+               ++pkt_num;
+               printf("%d ", pkt_num);
+               memcpy(buf, &pkt_num, sizeof(pkt_num));
+               sendto(sock, buf, PACKET_SIZE, 0, addr, addr_len);
+       }
+       printf("\n");
+}      
+
+int main(int argc, char **argv)
+{
+       int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+       struct sockaddr_in addr;
+       addr.sin_family = AF_INET;
+       addr.sin_addr.s_addr = inet_addr(argv[1]);
+       addr.sin_port = htons(atoi(argv[2]));
+
+       srand(time(NULL));
+
+       for ( ;; ) {
+               int burst_size = burst_sizes[rand() % num_burst_sizes];
+               send_burst(sock, (struct sockaddr *)&addr, sizeof(addr), burst_size);
+               sleep(1);
+       }
+}