sprintf(buf, "accept form %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
printf(buf, "");
for (i = 0; i< FD_SETSIZE; i++) {
if (clientSockFds[i] == -1) {
clientSockFds[i] = connfd;
break;
}
}
if (i == FD_SETSIZE) {
fprintf(stderr, "too many connection, more than %d\n", FD_SETSIZE);
close(connfd);
continue;
}
if (connfd > maxfd)
maxfd = connfd;
FD_SET(connfd, &allset);
if (--nready <= 0)
continue;
}
handle(clientSockFds, maxfd, &rset, &allset);
}
}
void handle(int * clientSockFds, int maxFds, fd_set* pRset, fd_set* pAllset) {
int nread;
int i;
char buf[MAXLINE];
for (i = 0; i< maxFds; i++) {
if (clientSockFds[i] != -1) {
if (FD_ISSET(clientSockFds[i], pRset)) {
memset(buf, '\0', MAXLINE);
nread = read(clientSockFds[i], buf, MAXLINE);//???????§??socket?÷
if (nread < 0) {
perror("read error");
close(clientSockFds[i]);
FD_CLR(clientSockFds[i], pAllset);
clientSockFds[i] = -1;
continue;
}
if (nread == 0) {
printf("client close the connection\n");
close(clientSockFds[i]);
FD_CLR(clientSockFds[i], pAllset);
clientSockFds[i] = -1;
continue;
}
printf("Receive message from %d, %s", clientSockFds[i], buf);