diff options
| -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 */ |
