Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "uv.h"
- static uv_loop_t *uv_loop;
- uv_udp_t handle;
- static void on_alloc(uv_handle_t* client, size_t suggested_size, uv_buf_t* buf) {
- buf->base = (char*) malloc(suggested_size);
- buf->len = suggested_size;
- }
- static void on_send(uv_udp_send_t *req, int status) {
- if (status) {
- fprintf(stderr, "Send error %s\n", uv_strerror(status));
- return;
- }
- }
- static void on_recv(uv_udp_t* handle, ssize_t nread, const uv_buf_t* rcvbuf, const struct sockaddr* addr, unsigned flags) {
- if (nread > 0) {
- printf("Length: %lu\n", nread);
- printf("Message: %s", rcvbuf->base);
- uv_udp_send_t req;
- uv_buf_t buf = uv_buf_init(rcvbuf->base, nread);
- uv_udp_send(&req, handle, &buf, 1, addr, on_send);
- }
- free(rcvbuf->base);
- }
- int main(int argc,char *argv[]) {
- struct sockaddr_in addr;
- uv_loop = uv_default_loop();
- uv_udp_init(uv_loop, &handle);
- uv_ip4_addr("0.0.0.0", 3000, &addr);
- uv_udp_bind(&handle, (const struct sockaddr*) &addr, 0);
- uv_udp_recv_start(&handle, on_alloc, on_recv);
- uv_run(uv_loop, UV_RUN_DEFAULT);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement