기본적으로 서버를 설정하고 새로운 클라이언트 (UNIX)를 받아들이고 select() 명령을 사용하여 파일 설명자의 작업을 기다리고 있지만 어떻게 작동하는지 기다리고 있습니다. 서버 측소켓을 사용하여 클라이언트 측에서 읽기
FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds);
FD_SET(STDIN_FILENO, &readfds);
while (1) {
testfds = readfds;
select(4 + MAXCLIENTS, &testfds, NULL, NULL, NULL);
for (fd = 0; fd < 4 + MAX_CLIENTS; fd++) {
if (FD_ISSET(fd, &testfds)) {
if (fd == server_sockfd) { /* new connection request */
client_sockfd = accept(server_sockfd, NULL, NULL);
if (num_clients < MAXCLIENTS) {
FD_SET(client_sockfd, &readfds);
num_clients++;
} else {
sprintf(message, "XSorry, too many clients. Try again later.\n");
write(client_sockfd, message, strlen(message));
close(client_sockfd);
}
} else if (fd == STDIN_FILENO) {
fgets(kb_message, BUFSIZ + 1, stdin);
if (strcmp(kb_message, "quit\n") == 0) {
sprintf(message, "XServer is shutting down.\n");
for (fd2 = 4; fd2 < 4 + MAX_CLIENTS; fd2++) {
if (FD_ISSET(fd2, &readfds)) {
write(fd2, message, strlen(message));
close(fd2);
}
}
close(server_sockfd);
exit(EXIT_SUCCESS);
} else {
sprintf(message, "M%s", kb_message);
for (fd2 = 4; fd2 < 4 + MAX_CLIENTS; fd2++)
if (FD_ISSET(fd2, &readfds))
write(fd2, message, strlen(message));
}
} else { /* client leaving */
close(fd);
FD_CLR(fd, &readfds);
num_clients--;
}
}
}
}에 그것을 읽고 다음 클라이언트 측에서 작성하는
는 "다른"방법에 따라 될 것 어떻게, 클라이언트로부터의 쓰기 요청을 처리 한 후 다시 그들에게 작성합니다 클라이언트가 종료 또는 작성 중인지 확인할 수 있습니까?
감사
@Roberto : 코드를 다시 편집하고 싶을 수도 있습니다. –