]> git.llucax.com Git - z.facultad/75.74/practicos.git/blob - practicas/pipi-2da-entrega/src/test_poll.c
Tag de 2da entrega.
[z.facultad/75.74/practicos.git] / practicas / pipi-2da-entrega / src / test_poll.c
1
2 #include "libtcp.h"
3 #include <assert.h>
4 #include <sys/poll.h>
5
6 #define MAX 10
7 #define PORT 5000
8
9 #define ISSET(var, flag) ((var & flag) == flag)
10
11 int main()
12 {
13         int ssock = libtcp_open_pasivo(PORT); assert(ssock != -1);
14         struct pollfd pfds[MAX];
15         pfds[0].fd = ssock;
16         pfds[0].events = POLLIN | POLLPRI;
17         int pfds_size = 1;
18         while (1)
19         {
20                 int res;
21                 res = poll(pfds, pfds_size, 5000); assert(res != -1);
22                 if (!res)
23                         continue;
24                 if (ISSET(pfds[0].revents, POLLIN)
25                                 || ISSET(pfds[0].revents, POLLPRI))
26                 {
27                         if (pfds_size == MAX)
28                         {
29                                 printf("Demasiadas conexiones, ignorando...\n");
30                                 continue;
31                         }
32                         pfds[pfds_size].fd = accept(ssock, NULL, NULL);
33                         assert(pfds[pfds_size].fd != -1);
34                         printf("Nueva conexión...\n");
35                         pfds[pfds_size].events = POLLIN | POLLPRI;
36                         ++pfds_size;
37                 }
38                 for (int i = 1; i < pfds_size; ++i)
39                 {
40                         if (ISSET(pfds[i].revents, POLLIN)
41                                         || ISSET(pfds[i].revents, POLLPRI))
42                         {
43                                 char buf[10];
44                                 int recibidos = libtcp_receive_bin(pfds[i].fd, buf, 10);
45                                 //assert(recibidos == 10);
46                                 if (recibidos)
47                                         printf("recibido %s\n", buf);
48                         }
49                 }
50         }
51 }
52