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
|
#include "dgrambuf.h"
#define _GNU_SOURCE
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
int open_test_socket();
/*
* Quick'n'dirty bash udp sender
* while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234; sleep 0.25; done
*/
int main() {
int res, sockfd, info;
dgrambuf_t dgb;
sockfd = open_test_socket();
dgb = dgrambuf_new(3, 50, 8, 8);
do {
res = dgrambuf_recvmmsg(dgb, sockfd, 1, &info);
printf("dgrambuf_recvmmsg() => %i\n", res);
printf("dgrambuf_free_count => %zu\n", dgrambuf_get_free_count(dgb));
} while ( res > 0 );
return 0;
}
int open_test_socket() {
int sockfd;
struct sockaddr_in sa;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket()");
exit(EXIT_FAILURE);
}
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sa.sin_port = htons(1234);
if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
perror("bind()");
exit(EXIT_FAILURE);
}
return sockfd;
}
|