在现代高性能服务器开发中,libuv 是一个不可或缺的跨平台异步 I/O 库。它最初为 Node.js 开发,如今被广泛用于各种需要高效处理并发 I/O 的 C/C++ 项目中。本教程将带你从零开始,使用 C语言 学习 libuv 的基本用法,即使你是编程小白也能轻松上手!
libuv 是一个支持事件循环、非阻塞 I/O、线程池、信号处理等特性的 C 语言库。它抽象了不同操作系统(如 Linux、Windows、macOS)的底层异步机制(如 epoll、kqueue、IOCP),让开发者可以用统一的 API 编写跨平台的异步编程代码。
在 Ubuntu/Debian 系统上,你可以通过以下命令安装:
sudo apt-get install libuv1-dev 在 macOS 上(需先安装 Homebrew):
brew install libuv 我们从最简单的例子开始——使用 libuv 创建一个定时器,在 1 秒后打印 “Hello, libuv!” 并退出。
#include <stdio.h>#include <uv.h>void timer_callback(uv_timer_t *handle) { printf("Hello, libuv!\n"); uv_stop(handle->loop); // 停止事件循环}int main() { uv_loop_t *loop = uv_default_loop(); uv_timer_t timer; uv_timer_init(loop, &timer); uv_timer_start(&timer, timer_callback, 1000, 0); printf("Starting event loop...\n"); uv_run(loop, UV_RUN_DEFAULT); return 0;} 编译命令(假设文件名为 hello.c):
gcc hello.c -luv -o hello 运行程序,你会看到:
Starting event loop...Hello, libuv! libuv 的核心是事件循环(Event Loop)。它不断检查是否有待处理的事件(如定时器到期、网络数据到达、文件读写完成等),并调用相应的回调函数。这种模型非常适合高并发场景,避免了传统多线程模型的开销。
下面是一个简单的 echo 服务器,它接收客户端连接,并将收到的数据原样返回:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <uv.h>#define DEFAULT_PORT 7000#define DEFAULT_BACKLOG 128void 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_write(uv_write_t *req, int status) { if (status) { fprintf(stderr, "Write error: %s\n", uv_strerror(status)); } free(req);}void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { if (nread > 0) { uv_write_t *req = (uv_write_t*) malloc(sizeof(uv_write_t)); uv_write(req, client, buf, 1, echo_write); free(buf->base); return; } if (nread < 0) { if (nread != UV_EOF) fprintf(stderr, "Read error: %s\n", uv_err_name(nread)); uv_close((uv_handle_t*) client, NULL); } 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(server->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, NULL); }}int main() { uv_loop_t *loop = uv_default_loop(); uv_tcp_t server; uv_tcp_init(loop, &server); struct sockaddr_in addr; 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("Echo server running on port %d\n", DEFAULT_PORT); uv_run(loop, UV_RUN_DEFAULT); uv_loop_close(loop); return 0;} 这个例子展示了如何使用 libuv 进行 网络编程。你可以用 telnet 或 nc 测试:
telnet localhost 7000 通过本教程,你已经学会了如何使用 libuv C语言 编写基本的异步程序,包括定时器和 TCP 服务器。libuv 强大的事件驱动模型使其成为构建高性能服务器的理想选择。继续深入学习,你可以探索文件系统操作、UDP、子进程管理等更多功能。
记住,掌握 异步编程 和 网络编程 是成为高级 C 开发者的关键一步。现在,就动手实践吧!
本文由主机测评网于2025-12-21发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20251210997.html