From d772f279e4e3823d08a64a9a910125072bfa7e6f Mon Sep 17 00:00:00 2001 From: cashmere Date: Thu, 25 Jun 2026 14:49:29 +0200 Subject: src/main.c: add comments and adjust variable names for readibility --- src/main.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 52a4a4a..85a884e 100644 --- a/src/main.c +++ b/src/main.c @@ -38,9 +38,10 @@ int main() { perror("could not establish connection"); return 1; } - fds[0].fd = sfd; /* socket file descriptor */ + + fds[0].fd = sfd; /* the listening socket */ fds[0].events = POLLIN; /* data to read */ - nfds_t nfds = 1; /* non file descriptor. something with timeout */ + nfds_t nfds = 1; /* 1 = listener alone, grows by 1 per connected client */ while (1) { /* we start the polling here */ int ready = @@ -49,8 +50,9 @@ int main() { perror("could not run poll"); return 1; } + /* if new events are detected by the socket listener */ if (fds[0].revents & POLLIN) { - /* we accept the connections */ + /* we accept the connection */ int conn = accept(sfd, NULL, NULL); if (conn == -1) { perror("could not accept connection"); @@ -65,20 +67,22 @@ int main() { for (nfds_t i = 1; i < nfds; i++) { /* if client has something to say */ if (fds[i].revents & POLLIN) { - char buf[1024]; - ssize_t n = read(fds[i].fd, buf, - sizeof(buf)); /* read the n which the client sent */ - if (n <= 0) { + char buffer[1024]; /* create a new empty buffer */ + ssize_t message = + read(fds[i].fd, buffer, + sizeof(buffer)); /* read the message which the client sent */ + /* if message of the client returns an error */ + if (message <= 0) { /* close the connection if client disappeared or disconnected */ close(fds[i].fd); fds[i] = fds[nfds - 1]; /* move back last slot */ - nfds--; /* size down the current aray */ + nfds--; /* size down the current array */ i--; } else { - /* send every n bytes to the other clients */ + /* send every message bytes to the other clients */ for (nfds_t j = 1; j < nfds; j++) { if (j != i) { - write(fds[j].fd, buf, n); + write(fds[j].fd, buffer, message); } } } -- cgit v1.3.1