满足以下条件 实现代码 1,使用ubus通信的notify进行发送信息 register来接收信息 2,使用epoll机制不是uloop机制 3,写出客户端和服务端代码
时间: 2024-03-10 16:45:35 浏览: 112
UDP服务端和客户端代码 客户端发送数据 服务端接受数据
好的,我明白了你的要求,下面是代码实现。
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <libubox/uloop.h>
#include <libubox/ustream.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
#define MAX_EVENTS 10
#define SERVER_PORT 6666
static struct ubus_context *ctx;
static int notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
const char *msg_str = blobmsg_data(blobmsg_get_field(msg, "msg"));
printf("Received message: %s\n", msg_str);
return UBUS_STATUS_OK;
}
static void uloop_socket_cb(struct uloop_fd *u, unsigned int events)
{
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(u->fd, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_fd < 0) {
perror("accept");
return;
}
printf("Accepted connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = client_fd;
if (epoll_ctl(u->epoll_fd, EPOLL_CTL_ADD, client_fd, &ev) < 0) {
perror("epoll_ctl");
close(client_fd);
return;
}
}
int main(int argc, char *argv[])
{
// Initialize ubus context
ctx = ubus_connect(NULL);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return EXIT_FAILURE;
}
// Register ubus object
static const struct ubus_method methods[] = {
{ .name = "notify", .handler = notify_cb },
};
static struct ubus_object_type obj_type = {
.name = "example_object",
.methods = methods,
.n_methods = ARRAY_SIZE(methods),
};
static struct ubus_object obj = {
.name = "example_object",
.type = &obj_type,
.methods = methods,
.n_methods = ARRAY_SIZE(methods),
};
if (ubus_add_object(ctx, &obj) != UBUS_STATUS_OK) {
fprintf(stderr, "Failed to add ubus object\n");
return EXIT_FAILURE;
}
// Initialize socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
return EXIT_FAILURE;
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
close(server_fd);
return EXIT_FAILURE;
}
if (listen(server_fd, 5) < 0) {
perror("listen");
close(server_fd);
return EXIT_FAILURE;
}
// Initialize epoll
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1");
close(server_fd);
return EXIT_FAILURE;
}
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = server_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &ev) < 0) {
perror("epoll_ctl");
close(server_fd);
close(epoll_fd);
return EXIT_FAILURE;
}
// Initialize uloop
struct uloop_fd ulfd;
memset(&ulfd, 0, sizeof(ulfd));
ulfd.cb = uloop_socket_cb;
ulfd.fd = server_fd;
ulfd.epoll_fd = epoll_fd;
if (uloop_fd_add(&ulfd, ULOOP_READ) < 0) {
perror("uloop_fd_add");
close(server_fd);
close(epoll_fd);
return EXIT_FAILURE;
}
// Start uloop main loop
uloop_run();
// Clean up
close(server_fd);
close(epoll_fd);
ubus_free(ctx);
return EXIT_SUCCESS;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#define MAX_EVENTS 10
#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT 6666
int main(int argc, char *argv[])
{
// Initialize socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
return EXIT_FAILURE;
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
server_addr.sin_port = htons(SERVER_PORT);
if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
close(server_fd);
return EXIT_FAILURE;
}
// Initialize epoll
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1");
close(server_fd);
return EXIT_FAILURE;
}
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = server_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &ev) < 0) {
perror("epoll_ctl");
close(server_fd);
close(epoll_fd);
return EXIT_FAILURE;
}
// Main loop
while (1) {
struct epoll_event events[MAX_EVENTS];
int num_events = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (num_events < 0) {
if (errno == EINTR)
continue;
else {
perror("epoll_wait");
break;
}
}
for (int i = 0; i < num_events; i++) {
if (events[i].events & EPOLLIN) {
char buf[1024];
int num_bytes = read(events[i].data.fd, buf, sizeof(buf));
if (num_bytes < 0) {
perror("read");
close(server_fd);
close(epoll_fd);
return EXIT_FAILURE;
}
else if (num_bytes == 0) {
printf("Connection closed by server\n");
close(server_fd);
close(epoll_fd);
return EXIT_SUCCESS;
}
else {
buf[num_bytes] = '\0';
printf("Received message: %s\n", buf);
}
}
}
}
// Clean up
close(server_fd);
close(epoll_fd);
return EXIT_SUCCESS;
}
```
阅读全文