xref: /haiku/src/tests/system/network/select_test.c (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 #include <stdio.h>
2 #include <kernel/OS.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <errno.h>
6 
7 #include "sys/socket.h"
8 #include "netinet/in.h"
9 #include "arpa/inet.h"
10 #include "sys/select.h"
11 
12 #include "ufunc.h"
13 
14 int main(int argc, char **argv)
15 {
16 	int s;
17 	int rv;
18 	struct fd_set fdr, fdw, fde;
19 	struct timeval tv;
20 	int32 rtc;
21 
22 	test_banner("Select test");
23 
24 	s = socket(AF_INET, SOCK_DGRAM, 0);
25 	if (s < 0)
26 		err(s, "Socket creation failed");
27 
28 	FD_ZERO(&fdr);
29 	FD_SET(s, &fdr);
30 	FD_ZERO(&fdw);
31 	FD_SET(s, &fdw);
32 	FD_ZERO(&fde);
33 	FD_SET(s, &fde);
34 
35 	tv.tv_sec = 5;
36 	tv.tv_usec = 0;
37 
38 	printf("Trying with timeval (5 secs)...\n");
39 	rtc = real_time_clock();
40 	rv = select(s + 1, &fdr, NULL, &fde, &tv);
41 	rtc = real_time_clock() - rtc;
42 	printf("select gave %d in %ld seconds\n", rv, rtc);
43 	if (rv < 0)
44 		printf("errno = %d [%s]\n", errno, strerror(errno));
45 
46 	printf("resetting select fd's\n");
47 	FD_ZERO(&fdr);
48 	FD_SET(s, &fdr);
49 	FD_ZERO(&fdw);
50 	FD_SET(s, &fdw);
51 	FD_ZERO(&fde);
52 	FD_SET(s, &fde);
53 
54 	printf("Trying without timeval (= NULL)\n");
55 	rv = select(s +1, &fdr, &fdw, &fde, NULL);
56 	printf("select gave %d\n", rv);
57 
58 	if (FD_ISSET(s, &fdr))
59 		printf("Data to read\n");
60 	if (FD_ISSET(s, &fdw))
61 		printf("OK to write\n");
62 	if (FD_ISSET(s, &fde))
63 		printf("Exception!\n");
64 
65 	closesocket(s);
66 
67 	printf("Test complete.\n");
68 
69 	return (0);
70 }
71 
72