Ticket #18539: sendtest.c

File sendtest.c, 1.0 KB (added by kohlschuetter, 14 months ago)

Example code demonstrating the bug

Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <sys/socket.h>
4#include <stdbool.h>
5
6int main() {
7 int fds[2];
8 int domain;
9 domain = AF_UNIX; // currently buggy: send hangs upon full buffer (no EBUFS, no silent drop)
10 // domain = AF_INET; // SOCK_DGRAM: packets get dropped silently (send continues)
11 printf("Domain: %i\n", domain);
12 int ret = socketpair(domain, SOCK_DGRAM, 0, fds); // try also: SOCK_STREAM
13 if(ret) {
14 perror("Could not get socketpair");
15 return 1;
16 }
17
18 struct timeval v = {
19 .tv_sec = 1,
20 .tv_usec = 0
21 };
22
23 // uncomment to allow send to timeout with ETIMEDOUT after 1 second
24 //ret = setsockopt(fds[0], SOL_SOCKET, SO_SNDTIMEO, &v, sizeof(v));
25 if(ret) {
26 perror("setsockopt");
27 }
28
29 size_t bufLen = 1024;
30 char *buf = calloc(bufLen, 1);
31 int ok = 0;
32 while(true) {
33 printf("send %i\n", ok);
34 ret = send(fds[0], &buf[0], bufLen, MSG_DONTWAIT);
35 // eventually: EAGAIN (on Linux), ENOBUFS (on macOS), hang (on Haiku)
36 if(ret < 0) {
37 perror("send");
38 break;
39 } else {
40 ok++;
41 }
42 }
43
44 return 0;
45}