static void on_connection(uv_stream_t* server, int status) { struct sockaddr sockname; int namelen = sizeof(sockname); uv_handle_t* handle; int r; if (status != 0) { fprintf(stderr, "Connect error %d\n", uv_last_error(loop).code); } ASSERT(status == 0); handle = (uv_handle_t*) malloc(sizeof(uv_tcp_t)); ASSERT(handle != NULL); uv_tcp_init(loop, (uv_tcp_t*)handle); /* associate server with stream */ handle->data = server; r = uv_accept(server, (uv_stream_t*)handle); ASSERT(r == 0); status = uv_getsockname(handle, &sockname, &namelen); if (status != 0) { fprintf(stderr, "uv_getsockname error (accepted) %d\n", uv_last_error(loop).code); } ASSERT(status == 0); getsocknamecount++; r = uv_read_start((uv_stream_t*)handle, alloc, after_read); ASSERT(r == 0); }
static void udp_recv(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned flags) { struct sockaddr sockname; char ip[20]; int namelen; int r; ASSERT(nread >= 0); if (nread == 0) { free(buf.base); return; } namelen = sizeof(sockname); r = uv_getsockname((uv_handle_t*)&udp, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (connector) %d\n", uv_last_error(loop).code); } ASSERT(r == 0); r = uv_ip4_name((struct sockaddr_in*)&sockname, ip, 20); ASSERT(r == 0); printf("sockname = %s\n", ip); getsocknamecount++; uv_close((uv_handle_t*) &udp, NULL); uv_close((uv_handle_t*) handle, NULL); }
static int tcp_listener(int port) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port); struct sockaddr sockname; int namelen = sizeof(sockname); int r; r = uv_tcp_init(&tcpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_tcp_bind(&tcpServer, addr); if (r) { fprintf(stderr, "Bind error\n"); return 1; } r = uv_tcp_listen(&tcpServer, 128, on_connection); if (r) { fprintf(stderr, "Listen error\n"); return 1; } r = uv_getsockname(&tcpServer, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (listening) %d\n", uv_last_error().code); } ASSERT(r == 0); getsocknamecount++; return 0; }
static void on_connect(void* req) { struct sockaddr sockname; int namelen = sizeof(sockname); int status; status = uv_getsockname(&tcp, &sockname, &namelen); if (status != 0) { fprintf(stderr, "uv_getsockname error (connector) %d\n", uv_last_error().code); } ASSERT(status == 0); getsocknamecount++; uv_close((uv_handle_t*)&tcp, NULL); }
static int tcp_listener(int port) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", port); struct sockaddr sockname; int namelen = sizeof(sockname); char ip[20]; int r; r = uv_tcp_init(loop, &tcpServer); if (r) { fprintf(stderr, "Socket creation error\n"); return 1; } r = uv_tcp_bind(&tcpServer, addr); if (r) { fprintf(stderr, "Bind error\n"); return 1; } r = uv_listen((uv_stream_t*)&tcpServer, 128, on_connection); if (r) { fprintf(stderr, "Listen error\n"); return 1; } memset(&sockname, -1, sizeof sockname); r = uv_getsockname((uv_handle_t*)&tcpServer, &sockname, &namelen); if (r != 0) { fprintf(stderr, "uv_getsockname error (listening) %d\n", uv_last_error(loop).code); } ASSERT(r == 0); r = uv_ip4_name((struct sockaddr_in*)&sockname, ip, 20); ASSERT(r == 0); ASSERT(ip[0] == '0'); ASSERT(ip[1] == '.'); ASSERT(ip[2] == '0'); printf("sockname = %s\n", ip); getsocknamecount++; return 0; }