#include "telnet.h" int main() { struct sockaddr_in addr; addr.sin_family = AF_INET; /* IPv4 */ addr.sin_port = htons(8000); /* Port */ addr.sin_addr.s_addr = INADDR_ANY; /* equals to 0.0.0.0 */ struct client clients[256]; struct pollfd fds[256]; int sfd = establish_connection(addr); if (sfd == -1) { perror("could not establish connection"); return 1; } fds[0].fd = sfd; /* the listening socket */ fds[0].events = POLLIN; /* data to read */ nfds_t nfds = 1; /* 1 = listener alone, grows by 1 per connected client */ while (1) { /* we start the polling here */ int ready = poll(fds, nfds, -1); /* poll to accept many conn simultaneously */ if (ready == -1) { perror("could not run poll"); return 1; } if (fds[0].revents & POLLIN) { if (handle_new_connection(sfd, fds, &nfds, clients) == -1) { perror("could not accept connection"); continue; } } if (process_message(fds, &nfds, clients) == -1) { perror("could not process message"); continue; } } return 0; }