Code:
while (1) {
testfds = readfds;
select(FD_SETSIZE, &testfds, NULL, NULL, NULL);
/* If there is activity, find which descriptor it's on using FD_ISSET */
for (fd = 0; fd < FD_SETSIZE; fd++) {
if (FD_ISSET(fd, &testfds)) {
if (fd == server_sockfd) { /* Accept a new connection request */
client_sockfd = accept(server_sockfd, NULL, NULL);
/*printf("client_sockfd: %d\n",client_sockfd);*/
if (num_clients < MAX_CLIENTS) {
FD_SET(client_sockfd, &readfds);
fd_array[num_clients]=client_sockfd;
/*Client ID*/
printf("Client %d joined\n",num_clients++);
fflush(stdout);
sprintf(msg,"M%d",client_sockfd);
/*write 2 byte clientID */
send(client_sockfd,msg,strlen(msg),0);
}
else {
sprintf(msg, "XSorry, too many clients. Try again later.\n");
write(client_sockfd, msg, strlen(msg));
close(client_sockfd);
}
}
else if (fd == 0) { /* Process keyboard activity */
fgets(kb_msg, MSG_SIZE + 1, stdin);
//printf("%s\n",kb_msg);
if (strcmp(kb_msg, "quit\n")==0) {
sprintf(msg, "XServer is shutting down.\n");
for (i = 0; i < num_clients ; i++) {
write(fd_array[i], msg, strlen(msg));
close(fd_array[i]);
}
close(server_sockfd);
exit(0);
}
else {
//printf("server - send\n");
sprintf(msg, "Message :%s", kb_msg);
for (i = 0; i < num_clients ; i++)
write(fd_array[i], msg, strlen(msg));
}
}
else if(fd) { /*Process Client specific activity*/
//printf("server - read\n");
//read data from open socket
result = read(fd, msg, MSG_SIZE);
if(result==-1) perror("read()");
else if(result>0){
/*read 2 bytes client id*/
sprintf(kb_msg,"M%d",fd); //2 change 1
msg[result]='\0';
/*concatinate the client id with the client's message*/
strcat(kb_msg,msg+1);
/*print to other clients*/
for(i=1;i<num_clients;i++){
if (fd_array[i] != fd) /*dont write msg to same client*/
write(fd_array[i],kb_msg,strlen(kb_msg));
}
/*print to server*/
printf("Print to Server:%s",kb_msg+1);
/*Exit Client*/
if(msg[0] == 'X'){
exitClient(fd,&readfds, fd_array,&num_clients);
}
}
}
else { /* A client is leaving */
exitClient(fd,&readfds, fd_array,&num_clients);
}//if
}//if
}//for
}//while
***wHICH part of the code can i extract the list of clients except myself of course, to be displayed at the right side of SERVER and CLIENT program?