/* ============================================================================ * mini_serv — community variant ("the sets + one-event-per-wakeup style"). * * Source: the version the user's friend passed exam 06 with. * Validated in this gym with: * - cc -Wall -Wextra -Werror * - tester/mini_serv_tester.py (13/13) * - the real exam-style Grademe diff (PASSED) * * Style differences vs reference/mini_serv.c: * - keeps a master fd_set (active_fds) and copies it into read_fds/write_fds * before select, instead of rebuilding from a slot array; * - processes ONE event per select() wakeup and `break`s (slower, but simpler); * - sends arrival/departure messages via a shared writebuff. * * Known cosmetic wart: guards `cl_sock >= 10240` while `clients[1024]` — with * select(2) fds are always < 1024, so the array cannot be overrun in practice. * ==========================================================================*/ #include #include #include #include #include #include #include typedef struct s_client { int id; char *msg; } t_client; static t_client clients[1024]; static char readbuff[100000], writebuff[100000]; static int max_fd = 0, next_id = 0; static fd_set active_fds, read_fds, write_fds; void exitError(char *str) { write(2, str, strlen(str)); exit(1); } void sendMsg(int sender_fd) { for (int fd = 0; fd <= max_fd; fd++) if (FD_ISSET(fd, &write_fds) && fd != sender_fd) send(fd, writebuff, strlen(writebuff), 0); } /* str_join and extract_message are given in the exam's main.c */ char *str_join(char *buf, char *add) { char *newbuf; int len; if (buf == 0) len = 0; else len = strlen(buf); newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1)); if (newbuf == 0) return (0); newbuf[0] = 0; if (buf != 0) strcat(newbuf, buf); free(buf); strcat(newbuf, add); return (newbuf); } int extract_message(char **buf, char **msg) { char *newbuf; int i; *msg = 0; if (*buf == 0) return (0); i = 0; while ((*buf)[i]) { if ((*buf)[i] == '\n') { newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1)); if (newbuf == 0) return (-1); strcpy(newbuf, *buf + i + 1); *msg = *buf; (*msg)[i + 1] = 0; *buf = newbuf; return (1); } i++; } return (0); } int main(int ac, char **av) { if (ac != 2) exitError("Wrong number of arguments\n"); int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) exitError("Fatal error\n"); max_fd = sockfd; FD_ZERO(&active_fds); FD_SET(sockfd, &active_fds); struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(2130706433); servaddr.sin_port = htons(atoi(av[1])); if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) exitError("Fatal error\n"); if (listen(sockfd, 10) != 0) exitError("Fatal error\n"); while (1) { read_fds = write_fds = active_fds; if (select(max_fd + 1, &read_fds, &write_fds, NULL, NULL) < 0) continue; for (int fd = 0; fd <= max_fd; fd++) { if (FD_ISSET(fd, &read_fds) && fd == sockfd) { int cl_sock = accept(sockfd, NULL, NULL); if (cl_sock < 0) continue; if (cl_sock >= 10240) { close(cl_sock); continue; } if (cl_sock > max_fd) max_fd = cl_sock; clients[cl_sock].id = next_id++; clients[cl_sock].msg = NULL; FD_SET(cl_sock, &active_fds); sprintf(writebuff, "server: client %d just arrived\n", clients[cl_sock].id); sendMsg(cl_sock); break; } else if (FD_ISSET(fd, &read_fds)) { int read_bytes = recv(fd, readbuff, sizeof(readbuff) - 1, 0); if (read_bytes <= 0) { sprintf(writebuff, "server: client %d just left\n", clients[fd].id); sendMsg(fd); FD_CLR(fd, &active_fds); close(fd); free(clients[fd].msg); break; } else { readbuff[read_bytes] = '\0'; clients[fd].msg = str_join(clients[fd].msg, readbuff); char *line = NULL; int res; while ((res = extract_message(&clients[fd].msg, &line)) > 0) { sprintf(writebuff, "client %d: ", clients[fd].id); char *str = str_join(NULL, writebuff); str = str_join(str, line); for (int cli_fd = 0; cli_fd <= max_fd; cli_fd++) if (FD_ISSET(cli_fd, &write_fds) && fd != cli_fd) send(cli_fd, str, strlen(str), 0); free(str); free(line); } } } } } }