]> git.sesse.net Git - bursty/blob - sender.c
Initial commit of sender.
[bursty] / sender.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <time.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10
11 #define PACKET_SIZE 1200
12
13 static int burst_sizes[] = { 1, 2, 3, 5, 10, 20, 50, 100 };
14 static int num_burst_sizes = sizeof(burst_sizes) / sizeof(burst_sizes[0]);
15
16 static void send_burst(int sock, const struct sockaddr *addr, socklen_t addr_len, int burst_size)
17 {
18         static char buf[PACKET_SIZE] = { 0 };
19         static int pkt_num = 0;
20         int i;
21
22         for (i = 0; i < burst_size; ++i) {
23                 ++pkt_num;
24                 printf("%d ", pkt_num);
25                 memcpy(buf, &pkt_num, sizeof(pkt_num));
26                 sendto(sock, buf, PACKET_SIZE, 0, addr, addr_len);
27         }
28         printf("\n");
29 }       
30
31 int main(int argc, char **argv)
32 {
33         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
34         struct sockaddr_in addr;
35         addr.sin_family = AF_INET;
36         addr.sin_addr.s_addr = inet_addr(argv[1]);
37         addr.sin_port = htons(atoi(argv[2]));
38
39         srand(time(NULL));
40
41         for ( ;; ) {
42                 int burst_size = burst_sizes[rand() % num_burst_sizes];
43                 send_burst(sock, (struct sockaddr *)&addr, sizeof(addr), burst_size);
44                 sleep(1);
45         }
46 }