Ticket #18548: recvtest.c

File recvtest.c, 870 bytes (added by kohlschuetter, 15 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;
10 // domain = AF_INET; // works
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 /*
19 struct timeval v = {
20 .tv_sec = 1,
21 .tv_usec = 0
22 };
23 ret = setsockopt(fds[0], SOL_SOCKET, SO_RCVTIMEO, &v, sizeof(v));
24 if(ret) {
25 perror("setsockopt");
26 }
27 */
28
29 size_t bufLen = 1024;
30 char *buf = calloc(bufLen, 1);
31 int ok = 0;
32 while(true) {
33 printf("recv %i\n", ok);
34 ret = recv(fds[0], &buf[0], bufLen, MSG_DONTWAIT);
35 // expected: EWOULDBLOCK/EAGAIN (as on Linux, macOS)
36 // Haiku: hangs
37 printf("%i\n", ret);
38 if(ret < 0) {
39 perror("recv");
40 break;
41 } else {
42 ok++;
43 }
44 }
45
46 return 0;
47}