1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <kernel/OS.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <errno.h>
7
8 #include "sys/socket.h"
9 #include "netinet/in.h"
10 #include "arpa/inet.h"
11 #include "sys/select.h"
12
13 #include "ufunc.h"
14
15 #define SOCKETS 15
16
main(int argc,char ** argv)17 int main(int argc, char **argv)
18 {
19 int * s;
20 int rv;
21 struct fd_set fdr, fdw, fde;
22 struct timeval tv;
23 int32 rtc;
24 int i, max = 0;
25 int nsock = SOCKETS;
26
27 test_banner("Big Select Test");
28
29 if (argc >= 2)
30 nsock = atoi(argv[1]);
31
32 if (nsock < 1 || nsock > 50) {
33 printf("Unrealistic value of sockets given. Must be between 1 and 50\n");
34 err(-1, "Parameters");
35 }
36
37 s = (int *) malloc(nsock * sizeof(int));
38 if (!s)
39 err(-1, "Not enought memory for sockets array");
40
41 printf("\nTest will be run with %d sockets\n\n", nsock);
42
43 for (i=0;i<nsock;i++) {
44 s[i] = socket(AF_INET, SOCK_DGRAM, 0);
45 if (s[i] < 0) {
46 free(s);
47 err(errno, "Socket creation failed");
48 }
49 }
50
51 FD_ZERO(&fdr);
52 for (i=0;i<nsock;i++) {
53 if (s[i] > max)
54 max = s[i];
55 FD_SET(s[i], &fdr);
56 }
57 FD_ZERO(&fdw);
58 // FD_SET(s, &fdw);
59 FD_ZERO(&fde);
60 // FD_SET(s, &fde);
61
62 tv.tv_sec = 5;
63 tv.tv_usec = 0;
64
65 printf("Trying with timeval (5 secs)...\n");
66 rtc = real_time_clock();
67 rv = select(max + 1, &fdr, NULL, &fde, &tv);
68 rtc = real_time_clock() - rtc;
69 printf("select gave %d in %ld seconds\n", rv, rtc);
70 if (rv < 0)
71 printf("errno = %d [%s]\n", errno, strerror(errno));
72
73 printf("resetting select fd's\n");
74 FD_ZERO(&fdr);
75 for (i=0;i<nsock;i++)
76 FD_SET(s[i], &fdr);
77 FD_ZERO(&fdw);
78 for (i=0;i<nsock;i++)
79 FD_SET(s[i], &fdw);
80 FD_ZERO(&fde);
81 for (i=0;i<nsock;i++)
82 FD_SET(s[i], &fde);
83
84 printf("Trying without timeval (= NULL)\n");
85 rv = select(max +1, &fdr, &fdw, &fde, NULL);
86 printf("select gave %d\n", rv);
87
88 if (rv > 0) {
89 if (FD_ISSET(s[0], &fdr))
90 printf("Data to read\n");
91 if (FD_ISSET(s[0], &fdw))
92 printf("OK to write\n");
93 if (FD_ISSET(s[0], &fde))
94 printf("Exception!\n");
95 } else if (rv < 0)
96 printf("error was %d [%s]\n", errno, strerror(errno));
97
98 for (i=0;i<nsock;i++)
99 closesocket(s[i]);
100
101 free(s);
102
103 printf("Test complete.\n");
104
105 return (0);
106 }
107
108