libuv udp server和client
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libuv udp server和client相关的知识,希望对你有一定的参考价值。
libuv
libuv是nodejs异步的io库,并非仅仅是网络,还包括文件,我们使用该库的异步通信机制来制作udp的server和client。
在windows平台上要包含一些lib
1 发送方
#include "uv.h"
#ifdef WIN32
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"Psapi.lib")
#pragma comment(lib,"Iphlpapi.lib")
#pragma comment(lib, "Userenv.lib")
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <chrono>
static void alloc_cb(uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf)
static char slab[1500];
buf->base = slab;
buf->len = sizeof(slab);
static void close_cb(uv_handle_t* handle)
uv_is_closing(handle);
static void cl_recv_cb(uv_udp_t* handle,
ssize_t nread,
const uv_buf_t* buf,
const struct sockaddr* addr,
unsigned flags)
if (nread < 0)
if (nread == 0)
return;
printf("I receive the message from server:%s\\n", buf->base);
uv_close((uv_handle_t*)handle, close_cb);
static void cl_send_cb(uv_udp_send_t* req, int status)
uv_udp_recv_start(req->handle, alloc_cb, cl_recv_cb);
#ifdef _WIN32
#include <windows.h>
void sleep(unsigned milliseconds)
Sleep(milliseconds);
#else
#include <unistd.h>
void sleep(unsigned milliseconds)
usleep(milliseconds * 1000); // takes microseconds
#endif
int main_sender()
static uv_udp_t client;
static uv_loop_t *loop;
static struct sockaddr_in addr;
loop = uv_default_loop();
int r;
r = uv_ip4_addr("0.0.0.0", 0, &addr);
r = uv_udp_init(loop, &client);
r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
uv_ip4_addr("127.0.0.1", 8899, &addr);
char buffer[1450];
unsigned int i = 0;
while (1)
uv_udp_send_t req;
uv_buf_t buf;
sprintf(buffer ,"the data is %d", i++);
buf = uv_buf_init((char*)buffer, strlen(buffer));
uv_udp_send(&req,
&client,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
sleep(1000);
return 0;
接收也就是服务端
所有函数都使用的回调方式,和发送端一样。
#include "udpsender.h"
#include "uv.h"
//#include "task.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct storage1
char flag;
char *pdata; //数据指针
storage1;
//申请一对空间
typedef struct storage
#define x_flag 1450
//最多40个人的服务
static char flag[40];
static char slab[58000];
storage;
//static uv_loop_t *loop;
static uv_udp_t server;
static void alloc_cb(uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf)
static char slab[65536];
buf->base = slab;
buf->len = sizeof(slab);
static void close_cb(uv_handle_t* handle)
uv_is_closing(handle);
static void sv_send_cb(uv_udp_send_t* req, int status)
uv_close((uv_handle_t*)req->handle, close_cb);
free(req);
static void sv_recv_cb(uv_udp_t* handle,
ssize_t nread,
const uv_buf_t* rcvbuf,
const struct sockaddr* addr,
unsigned flags)
// uv_udp_send_t* req;
// uv_buf_t sndbuf;
if (nread < 0)
if (nread == 0)
return;
printf("I receive the message from client:%s.\\n", rcvbuf->base);
uv_udp_recv_stop(handle);
//req = malloc(sizeof *req);
//sndbuf = uv_buf_init("PONG", 4);
//uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb);
int udpstart(uv_loop_t * loop)
struct sockaddr_in addr;
int r;
//loop = uv_default_loop();
r = uv_ip4_addr("localhost", 8899, &addr);
r = uv_udp_init(loop, &server);
r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
//uv_run(loop, UV_RUN_DEFAULT);
return 0;
以上是关于libuv udp server和client的主要内容,如果未能解决你的问题,请参考以下文章