使用 libevent 创建了一个简单的 TCP 服务器,C++实现,并有注释
时间: 2024-04-18 08:29:48 浏览: 27
下面是使用 libevent 库创建简单的 TCP 服务器的 C++ 实现代码,并附带注释:
```cpp
#include <iostream>
#include <event2/event.h>
#include <event2/listener.h>
void read_cb(struct bufferevent* bev, void* ctx) {
char buf[1024];
int n;
// 从客户端读取数据
while ((n = bufferevent_read(bev, buf, sizeof(buf) - 1)) > 0) {
buf[n] = '\0';
std::cout << "Received message: " << buf << std::endl;
// 向客户端发送回复消息
const char* replyMsg = "Server received your message\n";
bufferevent_write(bev, replyMsg, strlen(replyMsg));
}
}
void error_cb(struct bufferevent* bev, short error, void* ctx) {
if (error & BEV_EVENT_EOF) {
std::cout << "Connection closed." << std::endl;
} else if (error & BEV_EVENT_ERROR) {
std::cout << "Got an error on the connection: " << evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()) << std::endl;
}
// 释放 bufferevent 资源
bufferevent_free(bev);
}
void accept_conn_cb(struct evconnlistener* listener, evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx) {
struct event_base* base = evconnlistener_get_base(listener);
// 创建一个新的 bufferevent 用于处理连接
struct bufferevent* bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, read_cb, NULL, error_cb, NULL);
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
void accept_error_cb(struct evconnlistener* listener, void* ctx) {
struct event_base* base = evconnlistener_get_base(listener);
int err = EVUTIL_SOCKET_ERROR();
std::cerr << "Error " << err << ": " << evutil_socket_error_to_string(err) << std::endl;
event_base_loopexit(base, NULL);
}
int main() {
struct event_base* base = event_base_new();
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(8080);
// 创建一个监听器,并绑定到指定地址和端口
struct evconnlistener* listener = evconnlistener_new_bind(base, accept_conn_cb, NULL,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
-1, (struct sockaddr*)&sin, sizeof(sin));
if (!listener) {
perror("Failed to create listener");
return 1;
}
evconnlistener_set_error_cb(listener, accept_error_cb);
// 进入事件循环
event_base_dispatch(base);
// 释放资源
evconnlistener_free(listener);
event_base_free(base);
return 0;
}
```
这段代码实现了一个简单的 TCP 服务器,监听本地的 8080 端口。当有客户端连接时,服务器会接收客户端发送的消息,并将其打印出来。然后服务器会回复一个简单的消息给客户端。
希望这个示例对你有帮助!如果还有其他问题,请随时提问。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)