void select_for_soft_war(int * clients, fd_set * readfs, char * buffer) { int i; fd_set_clients(clients, readfs); select(8, readfs, NULL, NULL, NULL); for (i = 0; i < 4; i++) fd_isset_for(i, clients, readfs, buffer); if (FD_ISSET(0, readfs)) { for (i = 0; i < 4; i++) close(clients[i]); } }
int main(int argc, char **argv) { int port = 34034; int sock; fd_set fdset; int nfds; printf("Usage: %s [PORT]\n", argv[0]); if (argc > 1) { port = atoi(argv[1]); if (port < 1) port = 1; if (port > 0xFFFF) port = 0xFFFF; } printf("PORT %d\n", port); sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("Could not create socket"); return 1; } if (!bind_listen(sock, port)) { close(sock); return 1; } signal(SIGINT, sighandler); signal(SIGPIPE, sighandler); while (num_clients <= MAX_CLIENTS) { FD_ZERO(&fdset); FD_SET(sock, &fdset); nfds = sock+1; if (!num_clients) puts("Listening for connections..."); else nfds = fd_set_clients(&fdset, nfds); if (select(nfds, &fdset, 0,0,0) < 0 && errno != EINTR) { perror("select"); return 1; } read_from_clients(&fdset); if (FD_ISSET(sock, &fdset) && num_clients < MAX_CLIENTS) { accept_conn(sock); num_clients++; } } close(sock); return 0; }