#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);
}
#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) {
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);
}
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));
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);
}