#include #include #include #include #include #include #include #include #include #define BUFSIZE (64 * 1024) #define TOTAL_WRITE_SIZE (128 * 1024) void print_poll_revents(int revents) { printf("poll revents is %d:", revents); if (revents & POLLIN) { printf(" POLLIN"); } if (revents & POLLOUT) { printf(" POLLOUT"); } #ifdef POLLWRBAND if (revents & POLLWRBAND) { printf(" POLLWRBAND"); } #endif if (revents & POLLHUP) { printf(" POLLHUP"); } if (revents & POLLERR) { printf(" POLLERR"); } if (revents & POLLNVAL) { printf(" POLLNVAL"); } printf("\n"); } int main(int argc, char **argv) { struct sigaction act; int listen_sock; int accept_sock; struct sockaddr_in addr; int addr_len; int flags; struct pollfd pollfd; char buf[BUFSIZE]; int n; int ntotal; act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (sigaction(SIGPIPE, &act, NULL) == -1) { fprintf(stderr, "sigaction failed: %d\n", errno); exit(1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(0); listen_sock = socket(PF_INET, SOCK_STREAM, 0); if (listen_sock == -1) { fprintf(stderr, "socket failed: %d\n", errno); exit(1); } if (bind(listen_sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) { fprintf(stderr, "bind failed: %d\n", errno); exit(1); } addr_len = sizeof(addr); if (getsockname(listen_sock, (struct sockaddr *) &addr, &addr_len) == -1) { fprintf(stderr, "getsockname failed: %d\n", errno); exit(1); } printf("bound to port %d\n", ntohs(addr.sin_port)); fflush(stdout); if (listen(listen_sock, 5) == -1) { fprintf(stderr, "listen failed: %d\n", errno); exit(1); } while (1) { addr_len = sizeof(addr); accept_sock = accept(listen_sock, (struct sockaddr *) &addr, &addr_len); if (accept_sock == -1) { fprintf(stderr, "accept failed: %d\n", errno); exit(1); } printf("connection accepted\n"); flags = fcntl(accept_sock, F_GETFL, 0); if (flags == -1) { fprintf(stderr, "fcntl failed: %d\n", errno); exit(1); } flags |= O_NONBLOCK; if (fcntl(accept_sock, F_SETFL, flags) == -1) { fprintf(stderr, "fcntl failed: %d\n", errno); exit(1); } /* Read until end of stream */ pollfd.fd = accept_sock; pollfd.events = POLLIN; do { if (poll(&pollfd, 1, -1) == -1) { fprintf(stderr, "poll failed: %d\n", errno); exit(1); } print_poll_revents(pollfd.revents); n = read(accept_sock, buf, sizeof(buf)); if (n != -1) { printf("read %d bytes\n", n); } else { if (errno == EAGAIN) { printf("read failed with EAGAIN\n"); } else if (errno == EWOULDBLOCK) { printf("read failed with EWOULDBLOCK\n"); } else { fprintf(stderr, "read failed: %d\n", errno); exit(1); } } } while (n != 0); pollfd.events = POLLOUT; ntotal = 0; do { if (poll(&pollfd, 1, -1) == -1) { fprintf(stderr, "poll failed: %d\n", errno); exit(1); } print_poll_revents(pollfd.revents); n = write(accept_sock, buf, sizeof(buf)); if (n != -1) { printf("wrote %d bytes\n", n); ntotal += n; } else { if (errno == EAGAIN) { printf("write failed with EAGAIN\n"); } else if (errno == EWOULDBLOCK) { printf("write failed with EWOULDBLOCK\n"); } else if (errno == EPIPE) { printf("write failed with EPIPE\n"); break; } else { fprintf(stderr, "write failed: %d\n", errno); exit(1); } } } while (ntotal < TOTAL_WRITE_SIZE); if (close(accept_sock) == -1) { fprintf(stderr, "close failed: %d\n", errno); exit(1); } } return 0; }