📅  最后修改于: 2023-12-03 14:59:13.132000             🧑  作者: Mango
ALO - C++ 是一个基于 C++ 的高性能异步网络库,专为编写高并发服务器程序而设计。
#include <iostream>
#include "alo.h"
using namespace alo;
class EchoServer : public TcpServer {
public:
virtual void onMessage(ConnectionPtr conn, Buffer& buffer) override {
std::string message = buffer.toString();
conn->send(message.c_str(), message.size());
}
};
int main() {
EventLoop loop;
EchoServer server;
server.setConnectionCallback([](ConnectionPtr conn) {
std::cout << "New connection from " << conn->getPeerAddr().toIpPort() << std::endl;
});
server.setMessageCallback([](ConnectionPtr conn, Buffer& buffer) {
conn->send(buffer);
});
server.start(&loop, InetAddress(8888));
loop.loop();
return 0;
}
#include <iostream>
#include "alo.h"
using namespace alo;
class EchoClient : public TcpClient {
public:
virtual void onConnect(ConnectionPtr conn) override {
std::cout << "Connected to " << conn->getPeerAddr().toIpPort() << std::endl;
conn->send("Hello, world!");
}
virtual void onMessage(ConnectionPtr conn, Buffer& buffer) override {
std::cout << "Received message: " << buffer.toString() << std::endl;
conn->close();
}
virtual void onClose(ConnectionPtr conn) override {
std::cout << "Connection closed" << std::endl;
}
};
int main() {
EventLoop loop;
EchoClient client;
client.start(&loop, InetAddress("127.0.0.1", 8888));
loop.loop();
return 0;
}
ALO - C++ 是由开发者 JohnLi 李祺开发和维护的,欢迎提供代码贡献和使用反馈。