From d5da8a2b2a5a4fdf7aa713cee221ba717fc4746a Mon Sep 17 00:00:00 2001 From: cashmere Date: Thu, 25 Jun 2026 16:00:08 +0200 Subject: src/main.c: extract new connection into seperate fn --- src/main.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 85a884e..fc2815a 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,16 @@ int establish_connection(struct sockaddr_in addr) { return sfd; } +int handle_new_connection(int sfd, struct pollfd *fds, nfds_t *nfds) { + int conn = accept(sfd, NULL, NULL); + if (conn == -1) + return -1; + fds[*nfds].fd = conn; + fds[*nfds].events = POLLIN; + (*nfds)++; + return 0; +} + int main() { struct sockaddr_in addr; addr.sin_family = AF_INET; /* IPv4 */ @@ -50,19 +60,11 @@ 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 connection */ - int conn = accept(sfd, NULL, NULL); - if (conn == -1) { + if (handle_new_connection(sfd, fds, &nfds) == -1) { perror("could not accept connection"); - continue; /* if one client is not able to connect the server shall not - die */ + continue; } - fds[nfds].fd = conn; /* read */ - fds[nfds].events = - POLLIN; /* check if telnet client has something to say */ - nfds++; } for (nfds_t i = 1; i < nfds; i++) { /* if client has something to say */ -- cgit v1.3.1