Ticket #17714: socket.c

File socket.c, 960 bytes (added by selfish, 2 years ago)
Line 
1#include <sys/types.h>
2#include <sys/socket.h>
3#include <netdb.h>
4#include <stdio.h>
5#include <errno.h>
6#include <stdlib.h>
7#include <poll.h>
8
9void
10die(const char *funame)
11{
12 fprintf(stderr, "%s: %s\n", funame, strerror(errno));
13 exit(1);
14}
15
16int
17main()
18{
19 struct addrinfo hints, *res;
20 memset(&hints, 0, sizeof hints);
21 hints.ai_family = AF_INET;
22 hints.ai_socktype = SOCK_STREAM;
23 hints.ai_flags = AI_PASSIVE;
24
25 if(getaddrinfo(NULL, "0", &hints, &res) < 0)
26 die("getaddrinfo");
27
28 int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
29 if(sock < 0)
30 die("socket");
31
32 if(bind(sock, res->ai_addr, res->ai_addrlen) < 0)
33 die("bind");
34
35 if(listen(sock, 5) < 0)
36 die("listen");
37
38 struct pollfd fds[1];
39 fds[0].fd = sock;
40 fds[0].events = POLLOUT;
41 if(poll(fds, 1, 0) < 0)
42 die("poll");
43
44 // prints out, but shouldn't
45 if(fds[0].revents & POLLOUT)
46 printf("OUT\n");
47
48 return 0;
49}