if(newfd == -1) {
perror("accept");
}
else {
FD_SET(newfd, &master);
if(newfd > fdmax) {
fdmax = newfd;
}
printf("selectserver: new connection form %s on socket %d\n",
inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd);
}
}
else {
// if(i != newfd) {
// printf("you are in other's zone\n");
// goto fail;
// }
newfd = i; //注意这步!!!很重要!!!
while(1) {
printf("in the newfd round\n");
if((len = read(newfd, buf, 256)) <= 0) {
perror("read");
break;
}
printf("newfd i is %d\n", i);
// while((len = read(newfd, buf, 256)) > 0) {
printf("Message form client(%d): %s", len, buf);
if(buf[0] == '@')
break;
for(j = 0; j < len; j++) {
buf[j] = toupper(buf[j]);
}
write(newfd, buf, len);
memset(buf, 0, 256);
// printf("remove client on fd %d\n", newfd);
// close(newfd);
// FD_CLR(newfd, &master);
}
// FD_CLR(newfd, &read_fds);
printf("remove client on fd %d\n", newfd);
close(newfd);
FD_CLR(newfd, &master);
}
}
}
}
// fail: return 8;
}