xref: /haiku/src/tests/system/network/select_test2.c (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
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, f;
17 	int rv;
18 	struct fd_set fdr, fdw, fde;
19 	struct timeval tv;
20 	int32 rtc;
21 	char path[PATH_MAX];
22 
23 	test_banner("Select Test #2");
24 
25 	s = socket(AF_INET, SOCK_DGRAM, 0);
26 	if (s < 0)
27 		err(s, "Socket creation failed");
28 
29 	getcwd(path, PATH_MAX);
30 	sprintf(path, "%s/select_test2.c", path);
31 	f = open(path, O_RDWR);
32 
33 	if (f > 0 && s > 0)	{
34 		printf("\nsocket and fd created.\n");
35 	} else {
36 		err(-1, "Failed to create socket or fd\n");
37 	}
38 
39 	FD_ZERO(&fdr);
40 	FD_SET(s, &fdr);
41 	FD_ZERO(&fdw);
42 	FD_SET(s, &fdw);
43 	FD_ZERO(&fde);
44 	FD_SET(s, &fde);
45 
46 	tv.tv_sec = 5;
47 	tv.tv_usec = 0;
48 	printf("\nTest1\n=====\n\n");
49 	printf("Trying with timeval (5 secs)...\n");
50 	rtc = real_time_clock();
51 	rv = select(s + 1, &fdr, NULL, &fde, &tv);
52 	rtc = real_time_clock() - rtc;
53 	printf("select gave %d (expecting 0) in %ld seconds\n", rv, rtc);
54 
55 	FD_ZERO(&fdr);
56 	FD_SET(s, &fdr);
57 	FD_SET(f, &fdr);
58 	FD_ZERO(&fdw);
59 	FD_SET(s, &fdw);
60 	FD_ZERO(&fde);
61 	FD_SET(s, &fde);
62 
63 	printf("\nTest2\n=====\n\n");
64 	printf("Trying without timeval and both sockets and files...\n");
65 	rv = select(f +1, &fdr, NULL, NULL, NULL);
66 	printf("select gave %d (expecting 2)\n", rv);
67 
68 	if (rv > 0) {
69 		if (FD_ISSET(s, &fdr))
70 			printf("Data to read\n");
71 		if (FD_ISSET(s, &fdw))
72 			printf("OK to write\n");
73 		if (FD_ISSET(s, &fde))
74 			printf("Exception!\n");
75 		if (FD_ISSET(f, &fdr))
76 			printf("File is readable!\n");
77 	} else if (rv == 0) {
78 		printf("Timed out??? huh?!\n");
79 	} else {
80 		printf("errno = %d [%s]\n", errno, strerror(errno));
81 	}
82 
83 	closesocket(s);
84 	close(f);
85 
86 	printf("Test complete.\n");
87 
88 	return (0);
89 }
90 
91