]> git.sesse.net Git - bursty/blob - sender.c
Fix some corruption issues in truncate-end.pl.
[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, 200, 500 };
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_INET6, SOCK_DGRAM, IPPROTO_UDP);
34         struct sockaddr_in6 addr;
35         addr.sin6_family = AF_INET6;
36         addr.sin6_port = htons(atoi(argv[2]));
37
38         inet_pton(AF_INET6, argv[1], &addr.sin6_addr);
39
40         srand(time(NULL));
41
42         for ( ;; ) {
43                 int burst_size = burst_sizes[rand() % num_burst_sizes];
44                 send_burst(sock, (struct sockaddr *)&addr, sizeof(addr), burst_size);
45                 sleep(1);
46         }
47 }