xref: /haiku/src/tests/system/network/unix_recv_test.c (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/socket.h>
4 #include <stdbool.h>
5 
6 int 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 (on Linux, macOS, Haiku)
36 		printf("%i\n", ret);
37 		if(ret < 0) {
38 			perror("recv");
39 			break;
40 		} else {
41 			ok++;
42 		}
43 	}
44 
45 	return 0;
46 }
47