/* a single process concurrent server using select */ #include #include #include #include #include #include #define BUFSIZE 1024 int echo(int), SetupSocket(); void error(char *); int main(int argc, char *argv[]) { struct sockaddr_in from; int msock; /* master socket */ fd_set rfds; /* read file descriptors */ fd_set afds; /* active fds */ int alen; int fd, nfds, n; msock = SetupSocket(); FD_ZERO(&afds); FD_SET(msock,&afds); nfds = 4; /* number of open file descriptors plus 1 */ while (1) { memcpy((char *)&rfds, (char *)&afds, sizeof(rfds)); n = select(nfds,&rfds,NULL, NULL, NULL); if (n < 0) error("select"); if (FD_ISSET(msock, &rfds)) { int ssock; alen = sizeof(from); ssock = accept(msock, (struct sockaddr *) &from,&alen); if (ssock < 0) error("accept"); printf("Accepted a connection, the new socket is %d\n",ssock); FD_SET(ssock,&afds); nfds++; } for (fd = 0;fd