-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_udp.c
80 lines (67 loc) · 1.62 KB
/
send_udp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sock[10], err;
struct sockaddr_in sa, sa2;
socklen_t sa2_len;
int port = 123;
unsigned char buf[4096 * 100];
unsigned int bytes = 100000, rest, max;
unsigned int chunk_len = 1000;
unsigned int i, connections = 2;
char junk[128];
for (i = 0; i < connections; i++) {
sock[i] = socket(AF_INET, SOCK_DGRAM, 0);
if (sock[i] == -1) {
perror("socket");
return 1;
}
}
if (argc >= 2)
port = strtol(argv[1], NULL, 10);
if (argc >= 3)
bytes = strtol(argv[2], NULL, 10);
if (argc >= 4)
chunk_len = strtol(argv[3], NULL, 10);
memset(&sa, 0, sizeof(struct sockaddr));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
memset(buf, 'A', sizeof(buf));
rest = bytes;
while (rest > 0) {
max = sizeof(buf);
if (rest < max)
max = rest;
if (max > chunk_len)
max = chunk_len;
for (i = 0; i < connections; i++) {
printf("Sending %u bytes to connection %u...\n", max, i);
err = sendto(sock[i], buf, max, 0, (struct sockaddr *) &sa, sizeof(sa));
if (err == -1) {
perror("sendto");
break;
}
}
rest -= err;
}
for (i = 0; i < connections; i++) {
err = getsockname(sock[i], (struct sockaddr *) &sa2, &sa2_len);
if (err != 0) {
perror("getsockname");
return 1;
}
fprintf(stderr, "Socket bound to %s/%d.\n",
inet_ntop(sa2.sin_family, &sa2.sin_addr, junk, sa2_len),
ntohs(sa2.sin_port));
close(sock[i]);
}
return 0;
}