blob: 4fb15c57c481438486d6a9e05b804cacbdbec9a0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#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;
}
|