summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorcashmere <cashmere@autistici.org>2026-06-25 14:49:29 +0200
committercashmere <cashmere@autistici.org>2026-06-25 14:49:29 +0200
commitd772f279e4e3823d08a64a9a910125072bfa7e6f (patch)
tree7d98abd872f6918205a0b12ae709b72a15176e5d /src/main.c
parentc2e9c4e09db7e3ebfba9c925edcfd67e133fc0ec (diff)
downloadtelnet-d772f279e4e3823d08a64a9a910125072bfa7e6f.tar.gz
src/main.c: add comments and adjust variable names for readibility
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c24
1 files 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);
}
}
}