Guest User

Untitled

a guest
Oct 24th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include "uv.h"
  2. #include "stdio.h"
  3.  
  4. void on_new_connection(uv_stream_t *server, int status) {
  5.     if (status == -1) {
  6.         // error!
  7.         return;
  8.     }
  9.  
  10.     uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
  11.     uv_tcp_init(server->loop, client);
  12.     if (uv_accept(server, (uv_stream_t*) client) == 0) {
  13.         fprintf(stderr, "New client\n");
  14.     }
  15.     else {
  16.         uv_close((uv_handle_t*) client, NULL);
  17.     }
  18. }
  19.  
  20. int main(){
  21.     uv_loop_t *loop = uv_loop_new();
  22.  
  23.     uv_tcp_t server;
  24.     uv_tcp_init(loop, &server);
  25.  
  26.     struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
  27.     uv_tcp_bind(&server, bind_addr);
  28.     int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
  29.     if (r) {
  30.         fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
  31.         return 1;
  32.     }
  33.     uv_run(loop);
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment