blob: 1b96e3de6216158b9441a41d22c48a31a1b70765 (
plain)
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
|
#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=1, sockfd=open_test_socket();
dgrambuf_t dgb=dgrambuf_new(3, 50);
while (res > 0) {
res = dgrambuf_recvmmsg(dgb, sockfd);
printf("dgrambuf_recvmmsg() => %i\n", res);
printf("dgrambuf_free_count => %zi\n", dgrambuf_free_count(dgb));
}
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;
}
|