Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <string>
  8. #include <event2/event.h>
  9. #include <event.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12.  
  13. using namespace std;
  14.  
  15. #define SERVER_PORT 5185
  16.  
  17. struct client
  18. {
  19.     int fd;
  20.     struct bufferevent *buf_ev;
  21. };
  22.  
  23. int setnonblock(int fd)
  24. {
  25.     int flags;
  26.    
  27.     flags = fcntl(fd, F_GETFL);
  28.     flags |= O_NONBLOCK;
  29.     fcntl(fd, F_SETFL, flags);
  30. }
  31.  
  32. void buf_read_callback(struct bufferevent *incoming, void *arg)
  33. {
  34.     struct evbuffer *evreturn;
  35.     char *req;
  36.  
  37.     req = evbuffer_readline(incoming->input);
  38.     if (req == NULL)
  39.         return;
  40.  
  41.     evreturn = evbuffer_new();
  42.     evbuffer_add_printf(evreturn, "You said %s\n", req);
  43.     bufferevent_write_buffer(incoming, evreturn);
  44.     evbuffer_free(evreturn);
  45.     free(req);
  46. }
  47.  
  48. void buf_write_callback(struct bufferevent *bev, void *arg)
  49. {
  50. }
  51.  
  52. void buf_error_callback(struct bufferevent *bev, short what, void *arg)
  53. {
  54.     struct client *client = (struct client *)arg;
  55.     bufferevent_free(client->buf_ev);
  56.     close(client->fd);
  57.     free(client);
  58. }
  59.  
  60. void accept_callback(int fd, short ev, void *arg)
  61. {
  62.     int client_fd;
  63.     struct sockaddr_in client_addr;
  64.     socklen_t client_len = sizeof(client_addr);
  65.     struct client *client2;
  66.    
  67.     client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len);
  68.  
  69.     if (client_fd < 0)
  70.     {
  71.         fprintf(stderr, "failed accepting client\n");
  72.         return;
  73.     }
  74.  
  75.     setnonblock(client_fd);
  76.    
  77.     client2 = calloc(1, sizeof (*client));
  78.     if (client2 == NULL)
  79.         fprintf(stderr, "malloc failed\n");
  80.     client2->fd = client_fd;
  81.  
  82.     client2->buf_ev = bufferevent_new(client_fd, buf_read_callback, buf_write_callback, buf_error_callback, client2);
  83.     bufferevent_enable(client2->buf_ev, EV_READ);
  84. }
  85.  
  86. int main()
  87. {
  88.     printf("sizeof (structjunk) = %zu\n", sizeof(struct client));
  89.    
  90.     int socketlisten;
  91.     struct sockaddr_in addresslisten;
  92.     struct event accept_event;
  93.     int reuse = 1;
  94.    
  95.     event_init();
  96.  
  97.     socketlisten = socket(AF_INET, SOCK_STREAM, 0);
  98.  
  99.     if (socketlisten < 0)
  100.     {
  101.         fprintf(stderr, "Failed to create listen socket\n");
  102.         return 1;
  103.     }
  104.  
  105.     addresslisten.sin_family = AF_INET;
  106.     addresslisten.sin_addr.s_addr = INADDR_ANY;
  107.     addresslisten.sin_port = htons(SERVER_PORT);
  108.  
  109.     if (bind(socketlisten, (struct sockaddr *)&addresslisten, sizeof(addresslisten)) < 0)
  110.     {
  111.         fprintf(stderr, "failed to bind socket\n");
  112.         return 1;
  113.     }
  114.  
  115.     if (listen(socketlisten, 5) < 0)
  116.     {
  117.         fprintf(stderr, "failed to listen\n");
  118.         return 1;
  119.     }
  120.  
  121.     setsockopt(socketlisten, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  122.     setnonblock(socketlisten);
  123.  
  124.     event_set(&accept_event, socketlisten, EV_READ|EV_PERSIST, accept_callback, NULL);
  125.     event_add(&accept_event, NULL);
  126.     event_dispatch();
  127.  
  128.     close(socketlisten);
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement