-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathUDPLackFlowControlServer_BiggerBufferSpace.c
95 lines (80 loc) · 2.38 KB
/
UDPLackFlowControlServer_BiggerBufferSpace.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
static int totalRecvdCount;
static void recvfrom_handler(int signo)
{
printf("total recvd packet = %d\n", totalRecvdCount);
exit(0);
}
typedef void(*signal_handler)(int signo);
signal_handler fuck(int signo, signal_handler handler)
{
struct sigaction oldact, newact;
bzero(&newact, sizeof(newact));
newact.sa_handler = handler;
newact.sa_flags = 0;
sigemptyset(&newact.sa_mask);
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
newact.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
newact.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signo, &newact, &oldact) < 0)
return SIG_ERR;
return oldact.sa_handler;
}
int main(int argc, char const *argv[])
{
if (argc != 2 && argc != 3) {
printf("usage : %s <port> [sleep time ms, default 10]\n", argv[0]);
exit(1);
}
int fd = -1;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket fd create fail!");
exit(1);
}
fuck(SIGINT, recvfrom_handler);
size_t curRcvBuffSize = 0;
socklen_t len = sizeof(curRcvBuffSize);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &curRcvBuffSize, &len) < 0) {
perror("getsockopt error!");
exit(1);
}
printf("before increase recv buffer size = %zd %zd of 1400 bytes\n", curRcvBuffSize, curRcvBuffSize / 1400);
curRcvBuffSize *= 5;
printf("after increase recv buffer size = %zd %zd of 1400 bytes\n", curRcvBuffSize, curRcvBuffSize / 1400);
len = sizeof(curRcvBuffSize);
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &curRcvBuffSize, len) < 0) {
perror("setsockopt error!");
exit(1);
}
struct sockaddr_in svraddr, cliaddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(atoi(argv[1]));
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, (struct sockaddr *)&svraddr, sizeof(svraddr)) < 0) {
perror("bind error!");
exit(1);
}
char buf[3000];
for (;;) {
if (recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL) < 0) {
perror("recvfrom error!");
exit(1);
}
usleep(1000 * (argc == 3 ? atoi(argv[2]) : 10));
totalRecvdCount++;
}
return 0;
}