Guest User

Untitled

a guest
Jun 6th, 2013
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <netdb.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/event.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8.  
  9. int r;
  10.  
  11. int kq;
  12.  
  13. int nev;
  14.  
  15. int sockfd;
  16.  
  17. int conns = 0;
  18.  
  19. int clients[10];
  20.  
  21. char buffer[1024];
  22.  
  23. struct kevent events[2];
  24. struct kevent changes[2];
  25.  
  26. struct client_s {
  27.     int fd;
  28.     int type;
  29.     socklen_t addrlen;
  30.     struct sockaddr addr;
  31. };
  32.  
  33. int main(int argc, const char ** argv) {
  34.     struct addrinfo * addr = malloc(sizeof(struct addrinfo));
  35.     struct addrinfo * hints = malloc(sizeof(struct addrinfo));
  36.    
  37.     memset(changes, 0, sizeof(changes));
  38.     memset(events, 0, sizeof(events));
  39.  
  40.     hints->ai_family = AF_INET;
  41.     hints->ai_socktype = SOCK_STREAM;
  42.     hints->ai_protocol = IPPROTO_TCP;
  43.  
  44.     r = getaddrinfo("127.0.0.1", "3000", hints, &addr);
  45.  
  46.     if (r == -1) {
  47.         perror("Error on resolving address.\n");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.  
  51.     sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  52.  
  53.     if (sockfd == -1) {
  54.         perror("Error on creating socket descriptor.\n");
  55.         exit(EXIT_FAILURE);
  56.     }
  57.  
  58.     r = bind(sockfd, addr->ai_addr, addr->ai_addrlen);
  59.  
  60.     if (r == -1) {
  61.         perror("Error on binding to address.\n");
  62.         exit(EXIT_FAILURE);
  63.     }
  64.  
  65.     r = listen(sockfd, 1);
  66.  
  67.     if (r == -1) {
  68.         perror("Error on listening on socket.\n");
  69.         exit(EXIT_FAILURE);
  70.     }
  71.  
  72.     kq = kqueue();
  73.  
  74.     if (kq == -1) {
  75.         perror("Error on creating kqueue.\n");
  76.         exit(EXIT_FAILURE);
  77.     }
  78.    
  79.    
  80.  
  81.     EV_SET(&changes[0], sockfd, EVFILT_READ,
  82.             EV_ADD | EV_ENABLE, 0, 0, 0);
  83.  
  84.     for (;;) {
  85.         int c_n = 1;
  86.         if (changes[1].ident) {
  87.             c_n = 2;
  88.         }
  89.        
  90.         nev = kevent(kq, changes, c_n, events, 2, NULL);
  91.  
  92.         if (nev == -1) {
  93.             perror("Error on resolving kevents.\n");
  94.             exit(EXIT_FAILURE);
  95.         }
  96.        
  97.         printf("Got event\n");
  98.        
  99.         int i;
  100.         for (i = 0; i < nev; i++) {
  101.             struct client_s * client = malloc(sizeof(struct client_s));
  102.            
  103.             if (events[i].ident == sockfd) {
  104.                 printf("Accepting\n");
  105.                 client->fd = accept(sockfd, &client->addr, &client->addrlen);
  106.  
  107.                 if (client->fd == -1) {
  108.                     perror("Error on accepting client.\n");
  109.                     exit(EXIT_FAILURE);
  110.                 }
  111.  
  112.                 client->type = 2;
  113.  
  114.                 EV_SET(&changes[1], client->fd, EVFILT_READ,
  115.                         EV_ADD | EV_ENABLE, 0, 0, client);
  116.             }
  117.            
  118.             if (events[i].udata) {
  119.                 client = events[i].udata;
  120.  
  121.                 printf("fd %d, type %d\n", client->fd, client->type);
  122.  
  123.                 if (client->type == 2) {
  124.                     recv(client->fd, buffer, 1024, 0);
  125.                     printf("client says: %s\n", buffer);
  126.                 }
  127.             }
  128.         }
  129.     }
  130.  
  131.     close(sockfd);
  132.  
  133.     freeaddrinfo(addr);
  134.  
  135.     return EXIT_SUCCESS;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment