#include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sock; int efd; int one = 1; int port; int res; int optval; int optlen = sizeof(optval); struct sockaddr_in server; struct epoll_event ev; /* first argument is the destination addr as an ipv4 literal */ /* second argument is the destination port */ if (argc < 3) { printf("usage: %s \n", argv[0]); return 1; } port = atoi(argv[2]); efd = epoll_create1(O_CLOEXEC); if (efd < 0) { perror("epoll_create1"); return 1; } sock = socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP); if (sock < 0) { perror("socket"); return 1; } if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)) < 0) { perror("setsockopt"); return 1; } server.sin_addr.s_addr = inet_addr(argv[1]); server.sin_family = AF_INET; server.sin_port = htons(port); res = connect(sock, (struct sockaddr *)&server, sizeof(server)); if (res >= 0) { printf("connect succeeded, not interesting\n"); return 1; } if (errno != EINPROGRESS) { perror("connect"); return 1; } /* try to "miss" the epoll event */ sleep(5); ev.events = EPOLLIN|EPOLLOUT|EPOLLET|EPOLLRDHUP; ev.data.fd = sock; if (epoll_ctl(efd, EPOLL_CTL_ADD, sock, &ev) < 0) { perror("epoll_ctl"); return 1; } res = epoll_wait(efd, &ev, 1, 10000); if (res < 0) { perror("epoll_wait"); return 1; } if (res == 0) { printf("no events\n"); return 1; } if (res > 1) { printf("impossible number of events: %d\n", res); } if (getsockopt(ev.data.fd, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0) { perror("getsockopt"); } close(ev.data.fd); printf("socket fd %d has errno %d\n", ev.data.fd, optval); }