libuv,客户端,服务端

//client.cpp
#include <uv.h>
#include <iostream>
#include <thread>
#include <atomic>
#define DEFAULT_PORT 7000

uv_loop_t *loop;
uv_tcp_t client;
uv_connect_t connect_req;
uv_async_t event_async;

typedef struct {
	int id;
	char name[20];
	double value;
} MyStruct;



void on_write(uv_write_t *req, int status) {
	if (status == 0) {
		
		std::cout << "Write successful" << std::endl;
	}
	else {
		std::cerr << "Write error: " << uv_strerror(status) << std::endl;
	}
	
	free(req);
}



void send_data() {
	MyStruct data = { 123, "111", 3.14 };
	uv_buf_t buf = uv_buf_init((char *)&data, sizeof(MyStruct));
	uv_write_t *write_req = (uv_write_t *)malloc(sizeof(uv_write_t));
	uv_write(write_req, (uv_stream_t *)&client, &buf, 1, on_write);
}

void on_connect(uv_connect_t *req, int status) {
	if (status < 0) {
		fprintf(stderr, "Connect error: %s\n", uv_strerror(status));
		return;
	}

	printf("Connected to server\n");

}

std::atomic<int> cnt = 0;

void test_img() {

	while (true)
	{
		cnt++;
		
	}
}


void event_handler(uv_async_t* handle) {
	
	while(true) {
		if (cnt % 2000 == 0) {
			std::cout << cnt << std::endl;
			send_data();
			uv_sleep(1);
		}
	}
}


int main() {

	loop = uv_default_loop();
	uv_tcp_init(loop, &client);
	struct sockaddr_in dest;
	uv_ip4_addr("127.0.0.1", DEFAULT_PORT, &dest);
	uv_tcp_connect(&connect_req, &client, (const struct sockaddr *)&dest, on_connect);

	uv_async_init(loop, &event_async, event_handler);
	
	uv_async_send(&event_async);

	std::thread t1(test_img);
	t1.detach();

	return uv_run(loop, UV_RUN_DEFAULT);
}


//server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#include <iostream>

#define DEFAULT_PORT 7000
#define DEFAULT_BACKLOG 128

uv_loop_t *loop;
struct sockaddr_in addr;

typedef struct {
	int id;
	char name[20];
	double value;
} MyStruct;

void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
	buf->base = (char*)malloc(suggested_size);
	buf->len = suggested_size;
}

void on_close(uv_handle_t* handle) {
	free(handle);
}

void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
	if (nread > 0) {
		// Assuming the received data is a MyStruct
		MyStruct data;
		memcpy(&data, buf->base, sizeof(MyStruct));
		printf("Received data from client: ID=%d, Name=%s, Value=%f\n", data.id, data.name, data.value);
	/*	return;*/
	}
	else if (nread < 0) {
		if (nread != UV_EOF)
			fprintf(stderr, "Read error %s\n", uv_err_name(nread));

		uv_close((uv_handle_t*)client, on_close);
	}

	free(buf->base);
}

void on_new_connection(uv_stream_t *server, int status) {
	if (status < 0) {
		fprintf(stderr, "New connection error %s\n", uv_strerror(status));
		// error!
		return;
	}
	uv_tcp_t *client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
	uv_tcp_init(loop, client);
	if (uv_accept(server, (uv_stream_t*)client) == 0) {
		uv_read_start((uv_stream_t*)client, alloc_buffer, echo_read);
	}
	else {
		uv_close((uv_handle_t*)client, on_close);
	}
}

int main() {

	loop = uv_default_loop();
	uv_tcp_t server;
	uv_tcp_init(loop, &server);
	uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr);
	uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);

	int r = uv_listen((uv_stream_t*)&server, DEFAULT_BACKLOG, on_new_connection);

	if (r) {
		fprintf(stderr, "Listen error %s\n", uv_strerror(r));
		return 1;
	}

	printf("Server listening on port %d\n", DEFAULT_PORT);

	return uv_run(loop, UV_RUN_DEFAULT);
}
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值