#include #include #include #include fatal(char *s) { perror(s); exit(1); } main() { int lsock; struct sockaddr_in saddr; int conn; lsock = socket(AF_INET, SOCK_STREAM, PF_UNSPEC); if (lsock == -1) { fatal("socket"); } saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(12000); if (bind(lsock, &saddr, sizeof(saddr)) == -1) fatal("bind"); if (listen(lsock, 5) == -1) fatal("listen"); for (;;) { int on = 1; conn = accept(lsock, 0, 0); if (conn == -1) fatal("accept"); ioctl(conn, FIONBIO, &on); for (;;) { struct pollfd pfd; int buf[800]; pfd.fd = conn; pfd.events = POLLIN; if (poll(&pfd, 1, 10000) == 0) break; read(conn, buf, sizeof(buf)); } close(conn); } }