基于libuv库的UDP收/发广播消息代码实现
uv_send(发送端):
1 #include "uv.h" 2 #include "task.h" 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 static uv_udp_t client; 9 10 static uv_loop_t *loop; 11 12 static void alloc_cb(uv_handle_t* handle, 13 size_t suggested_size, 14 uv_buf_t* buf) 15 { 16 static char slab[65536]; 17 buf->base = slab; 18 buf->len = sizeof(slab); 19 } 20 21 static void close_cb(uv_handle_t* handle) 22 { 23 uv_is_closing(handle); 24 } 25 26 static void cl_recv_cb(uv_udp_t* handle, 27 ssize_t nread, 28 const uv_buf_t* buf, 29 const struct sockaddr* addr, 30 unsigned flags) 31 { 32 if (nread < 0) 33 { 34 35 } 36 37 if (nread == 0) 38 { 39 return; 40 } 41 42 printf("I receive the message from server:%s\n", buf->base); 43 44 uv_close((uv_handle_t*) handle, close_cb); 45 } 46 47 48 static void cl_send_cb(uv_udp_send_t* req, int status) 49 { 50 uv_udp_recv_start(req->handle, alloc_cb, cl_recv_cb); 51 } 52 53 int main(void) 54 { 55 struct sockaddr_in addr; 56 uv_udp_send_t req; 57 uv_buf_t buf; 58 int r; 59 60 loop = uv_default_loop(); 61 62 r = uv_ip4_addr("0.0.0.0", 0, &addr); 63 r = uv_udp_init(loop, &client); 64 r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0); 65 r = uv_udp_set_broadcast(&client, 1); 66 uv_ip4_addr("255.255.255.255", 8899, &addr); 67 buf = uv_buf_init("PING", 4); 68 printf("I am sending message to server:%s.\n", buf.base); 69 uv_udp_send(&req, 70 &client, 71 &buf, 72 1, 73 (const struct sockaddr*) &addr, 74 cl_send_cb); 75 76 uv_run(loop, UV_RUN_DEFAULT); 77 78 return 0; 79 }
uv_recv(接收端):
1 #include "uv.h" 2 #include "task.h" 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 static uv_loop_t *loop; 9 static uv_udp_t server; 10 11 static void alloc_cb(uv_handle_t* handle, 12 size_t suggested_size, 13 uv_buf_t* buf) 14 { 15 static char slab[65536]; 16 17 buf->base = slab; 18 buf->len = sizeof(slab); 19 } 20 21 static void close_cb(uv_handle_t* handle) 22 { 23 uv_is_closing(handle); 24 } 25 26 static void sv_send_cb(uv_udp_send_t* req, int status) 27 { 28 uv_close((uv_handle_t*) req->handle, close_cb); 29 free(req); 30 } 31 32 static void sv_recv_cb(uv_udp_t* handle, 33 ssize_t nread, 34 const uv_buf_t* rcvbuf, 35 const struct sockaddr* addr, 36 unsigned flags) 37 { 38 uv_udp_send_t* req; 39 uv_buf_t sndbuf; 40 41 if (nread < 0) 42 { 43 44 } 45 46 if (nread == 0) 47 { 48 return; 49 } 50 51 printf("I receive the message from client:%s.\n", rcvbuf->base); 52 53 uv_udp_recv_stop(handle); 54 55 req = malloc(sizeof *req); 56 57 sndbuf = uv_buf_init("PONG", 4); 58 uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb); 59 } 60 61 int main(void) 62 { 63 struct sockaddr_in addr; 64 uv_udp_send_t req; 65 uv_buf_t buf; 66 int r; 67 68 loop = uv_default_loop(); 69 r = uv_ip4_addr("0.0.0.0", 8899, &addr); 70 r = uv_udp_init(loop, &server); 71 r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0); 72 r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb); 73 74 uv_run(loop, UV_RUN_DEFAULT); 75 76 return 0; 77 }