1. 定时器使用
#include <stdio.h>
#include <libubox/uloop.h>
void my_timer_callback(struct uloop_timeout *timeout) {
printf("Timer trigger.\n");
uloop_timeout_set(timeout, 2000); // 重置定时器
}
int main()
{
uloop_init();
struct uloop_timeout my_timer = {
.cb = my_timer_callback,
};
uloop_timeout_set(&my_timer, 2000); // 每隔2秒触发一次
uloop_run();
uloop_done();
return 0;
}
--------------------------------------------------------------------------------------
2 socket
#include <libubox/uloop.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#define PORT 12345
// 事件回调函数,当socket有数据可读时触发
void read_callback(struct uloop_fd *fd, unsigned int events) {
char buffer[128];
int len = read(fd->fd, buffer, sizeof(buffer) - 1);
if (len > 0) {
buffer[len] = '\0';
printf("Received data: %s\n", buffer);
} else if (len == 0) {
printf("Connection closed by peer.\n");
uloop_fd_delete(fd); // 删除文件描述符
close(fd->fd); // 关闭socket
free(fd); // 释放内存
} else {
perror("Error reading data");
uloop_fd_delete(fd); // 删除文件描述符
close(fd->fd); // 关闭socket
free(fd); // 释放内存
}
}
// 事件回调函数,当socket可接受连接时触发
void server_callback(struct uloop_fd *fd, unsigned int events) {
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int client_sock = accept(fd->fd, (struct sockaddr *)&client_addr, &addr_len);
if (client_sock < 0) {
perror("Accept failed");
return;
}
printf("Client connected\n");
// 设置为非阻塞模式
int flags = fcntl(client_sock, F_GETFL, 0);
fcntl(client_sock, F_SETFL, flags | O_NONBLOCK);
// 为新客户端创建 uloop_fd 结构
struct uloop_fd *client_fd = malloc(sizeof(struct uloop_fd));
client_fd->fd = client_sock;
client_fd->cb = read_callback;
// 将客户端的文件描述符添加到 uloop 中,监听可读事件
uloop_fd_add(client_fd, ULOOP_READ);
}
int main() {
struct sockaddr_in server_addr;
// 初始化 uloop 库
uloop_init();
// 创建服务器 socket
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
perror("Socket creation failed");
return -1;
}
// 设置服务器 socket 为非阻塞模式
int flags = fcntl(server_sock, F_GETFL, 0);
fcntl(server_sock, F_SETFL, flags | O_NONBLOCK);
// 绑定 socket 到端口
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(server_sock);
return -1;
}
// 监听连接
if (listen(server_sock, 5) < 0) {
perror("Listen failed");
close(server_sock);
return -1;
}
printf("Server listening on port %d\n", PORT);
// 为服务器 socket 创建 uloop_fd 结构
struct uloop_fd server_fd;
server_fd.fd = server_sock;
server_fd.cb = server_callback;
// 将服务器 socket 添加到 uloop 中,监听可读事件
uloop_fd_add(&server_fd, ULOOP_READ);
// 运行 uloop 事件循环
uloop_run();
// 清理
uloop_done();
close(server_sock);
return 0;
}
---------------------------------------------------------------
3 信号
#include <stdio.h>
#include <libubox/uloop.h>
// 信号回调函数
void my_signal_callback(int signo, void *data) {
printf("Received signal %d, exiting...\n", signo);
uloop_end(); // 结束事件循环
}
int main() {
uloop_init();
// 3. 设置信号事件,监听 SIGINT 信号
struct uloop_signal my_signal = {
.signo = SIGINT,
.cb = my_signal_callback,
};
uloop_signal_add(&my_signal);
printf("Starting event loop...\n");
uloop_run();
// 清理资源
uloop_done();
printf("Event loop ended.\n");
}
--------------------------------------------------------------------------------------
4 父子进程
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <libubox/uloop.h>
// 子进程回调函数
void my_process_callback(struct uloop_process *process, int ret) {
printf("Child process %d exited with code %d getpid=%d.\n", process->pid, ret, getpid());
uloop_end(); // 结束事件循环
}
int main()
{
uloop_init();
// 4. 创建子进程并监听其退出事件
pid_t pid = fork();
if (pid == 0) {
// 子进程代码
sleep(5); // 模拟任务执行
exit(0); // 子进程退出
} else {
// 父进程中添加子进程监听
struct uloop_process my_process = {
.pid = pid,
.cb = my_process_callback,
};
printf("parent1\n");
uloop_process_add(&my_process);
printf("parent2\n");//父进程可以继续执行不用阻塞等待子进程的退出
}
printf("Starting event loop...\n");
uloop_run();
uloop_done();
printf("Event loop ended.\n");
return 0;
}
标签:addr,sock,server,uloop,fd,使用,include,libubox From: https://www.cnblogs.com/xpylovely/p/18541495