7 #include "sys/socket.h"
8 #include "netinet/in.h"
10 #include "sys/select.h"
14 int main(int argc
, char **argv
)
18 struct fd_set fdr
, fdw
, fde
;
23 test_banner("Select Test #2");
25 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
27 err(s
, "Socket creation failed");
29 getcwd(path
, PATH_MAX
);
30 sprintf(path
, "%s/select_test2.c", path
);
31 f
= open(path
, O_RDWR
);
34 printf("\nsocket and fd created.\n");
36 err(-1, "Failed to create socket or fd\n");
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
);
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
);
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");
78 printf("Timed out??? huh?!\n");
80 printf("errno = %d [%s]\n", errno
, strerror(errno
));
86 printf("Test complete.\n");