diff options
| author | cashmere <cashmere@autistici.org> | 2026-06-25 16:00:08 +0200 |
|---|---|---|
| committer | cashmere <cashmere@autistici.org> | 2026-06-25 16:00:08 +0200 |
| commit | d5da8a2b2a5a4fdf7aa713cee221ba717fc4746a (patch) | |
| tree | d4220aa64a1c45c9105c2a24813421f27a5c2813 | |
| parent | d772f279e4e3823d08a64a9a910125072bfa7e6f (diff) | |
| download | telnet-d5da8a2b2a5a4fdf7aa713cee221ba717fc4746a.tar.gz | |
src/main.c: extract new connection into seperate fn
| -rw-r--r-- | src/main.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -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 */ |
