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