基于libuv的TCP echo-server

这篇博客介绍了如何利用libuv库构建一个TCP echo-server。作者参考了libuv源码中的test/echo-server.c文件,并添加了自己的代码,使得服务器在接收到客户端的奇数次输入时原样返回,偶数次则返回输入文字的逆序。通过这个例子,读者可以学习到如何在异步非阻塞模式下,使用libuv进行socket编程,包括创建socket、bind、listen、accept等步骤,以及如何注册连接回调、读写函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习libuv库,写TCP echo-server

大部分代码都来自libuv源码test目录下echo-server.c文件。

我自己添加了一点代码,通过uv_read_start函数更改read_cb函数

使得第奇数次客户端输入一行文字后,服务端原样返回,

偶数次输入一行文字,服务端将输入的一行文字逆序后返回

结果如下:

cheng@debian:~$ telnet 127.0.0.1 7890
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
helo
helo
helo
oleh
00000111111
00000111111
00000111111
11111100000


---------------------------------------------------------总结的分割线---------------------------------------------------------------------------------

通过echo-server这个程序,可以学习怎么利用libuv,像完成原始socket网络编程那样,

创建socket, bind, listen, accept过程。

只是在异步,非阻塞的方式编程时,需要通过事先为某个事件发生时应该调用的回调

在echo-server程序中,通过uv_listen函数注册连接回调函数

uv_read_start注册读函数

uv_write注册写函数

在头文件uv.h看这三个函数的函数原型,可以很清楚的知道每个参数的意义

------------------------------------------------------------------------------------------------------------------------------------------------------------

修改过的echo-server.c代码:

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERC
### 使用 libuv 进行 TCP 开发 #### 创建事件循环 为了使用 `libuv` 的功能,首先需要创建一个事件循环实例。这可以通过调用 `uv_loop_t *loop = uv_default_loop();` 来完成。 ```c #include <uv.h> int main() { uv_loop_t *loop = uv_default_loop(); } ``` #### 初始化TCP句柄 初始化一个新的 TCP 句柄用于监听传入连接或建立新的客户端连接: ```c uv_tcp_t server; uv_tcp_init(loop, &server); ``` #### 绑定地址并监听端口 设置服务器套接字选项,并绑定到指定 IP 地址和端口号上准备接收新连接请求[^1]。 ```c struct sockaddr_in addr; uv_ip4_addr("0.0.0.0", 7000, &addr); uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0); int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection); if (r) { fprintf(stderr, "Listen error %s\n", uv_strerror(r)); return 1; } ``` #### 处理新连接 当有新的客户端尝试连接时会触发回调函数,在此定义处理逻辑来接受这些连接。 ```c void on_new_connection(uv_stream_t* server, int status) { if (status < 0) { fprintf(stderr, "New connection error %s\n", uv_strerror(status)); // Handle errors here. 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 free(client); } ``` #### 数据读取与写入操作 实现数据传输过程中的读缓冲区分配以及实际的数据读取消息回显给发送方的功能。 ```c 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 echo_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) { if (nread < 0) { if (nread != UV_EOF) fprintf(stderr, "Read error %s\n", uv_err_name(nread)); uv_close((uv_handle_t*)tcp, NULL); free(buf->base); return; } printf("Received data: %.*s\n", (int)nread, buf->base); uv_write_t* req = new uv_write_t; uv_buf_t wrbuf = uv_buf_init(buf->base, nread); uv_write(req, tcp, &wrbuf, 1, after_write); } void after_write(uv_write_t* req, int status) { delete req; } ``` 通过上述代码片段展示了基于 `libuv` 库构建简单 TCP 服务端应用程序的方法,包括启动事件循环、配置网络接口参数、响应客户接入请求及双向通信等功能模块的设计思路[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值