💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ### 异步服务器 ***** #### OS.cpp ```#include <iostream> #include <string.h> #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> using namespace boost::asio; using boost::asio::ip::tcp; using boost::asio::ip::address; class session : public boost::enable_shared_from_this<session> { public: session(boost::asio::io_service &io_service) : socket_(io_service) { } void start() { static tcp::no_delay option(true); socket_.set_option(option); std::cout << "Client Accept:" << socket_.remote_endpoint().address().to_string() << ":" << socket_.remote_endpoint().port() << std::endl; boost::asio::async_read_until(socket_, sbuf_, "\n", boost::bind(&session::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } tcp::socket &socket() { return socket_; } private: void handle_write(const boost::system::error_code& error, size_t bytes_transferred) { boost::asio::async_read_until(socket_, sbuf_, "\n", boost::bind(&session::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); if (sbuf_.size() > 0) { boost::asio::streambuf::const_buffers_type cbt = sbuf_.data(); std::string str(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); std::cout << "handle_write-- " << str.c_str() << std::endl; } } void handle_read(const boost::system::error_code& error, size_t bytes_transferred) { boost::asio::async_write(socket_, sbuf_, boost::bind(&session::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); if (sbuf_.size() > 0) { boost::asio::streambuf::const_buffers_type cbt = sbuf_.data(); std::string str(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); std::cout << "handle_read-- " << str.c_str() << std::endl; } } private: tcp::socket socket_; boost::asio::streambuf sbuf_; }; typedef boost::shared_ptr<session> session_ptr; class server { public: server(boost::asio::io_service &io_service) : io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(),6661)) { nostop = true; retry = 0; } void accept() { session_ptr new_session(new session(io_service_)); acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error)); } void handle_accept(session_ptr new_session, const boost::system::error_code& error) { if (error) { std::cout << error.message() << std::endl; return; } new_session->start(); session_list_.push_back(new_session); retry++; std::cout << "第" << retry << "个连接进来" << std::endl; accept(); /*new_session.reset(new session(io_service_)); acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error));*/ } void run() { io_service_.run(); } private: bool nostop; boost::asio::io_service &io_service_; tcp::acceptor acceptor_; int retry; std::list<session_ptr> session_list_; }; int main(int argc, char* argv[]) { boost::asio::io_service io_service; server s(io_service); s.accept(); s.run(); return 0; } ``` #### OC.cpp ``` #include<boost/asio/io_service.hpp> #include<boost/asio/ip/tcp.hpp> #include<boost/bind.hpp> #include<boost/shared_ptr.hpp> #include<boost/enable_shared_from_this.hpp> #include<string> #include<iostream> #include<boost/asio/streambuf.hpp> #include<boost/asio/placeholders.hpp> #include<boost/asio.hpp> using boost::asio::ip::tcp; using boost::asio::ip::address; class client : public boost::enable_shared_from_this<client> { public: client(boost::asio::io_service &io_service, tcp::endpoint &endpoint) : io_service_(io_service), socket_(io_service), endpoint_(endpoint) { } void start() { socket_.async_connect(endpoint_, boost::bind(&client::handle_connect, shared_from_this(), boost::asio::placeholders::error)); } private: void handle_connect(const boost::system::error_code &error) { if (error) { if (error.value() != boost::system::errc::operation_canceled) { std::cerr << boost::system::system_error(error).what() << std::endl; } socket_.close(); return; } static tcp::no_delay option(true); socket_.set_option(option); strcpy(buf, "Hello World!\n"); boost::asio::async_write(socket_, boost::asio::buffer(buf, strlen(buf)), boost::bind(&client::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void handle_write(const boost::system::error_code& error, size_t bytes_transferred) { memset(buf, sizeof(buf), 0); boost::asio::async_read_until(socket_, sbuf, "\n", boost::bind(&client::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } void handle_read(const boost::system::error_code& error, size_t bytes_transferred) { std::cout << buf << std::endl; } private: boost::asio::io_service &io_service_; tcp::socket socket_; tcp::endpoint &endpoint_; char buf[1024]; boost::asio::streambuf sbuf; }; typedef boost::shared_ptr<client> client_ptr; int main(int argc, char* argv[]) { boost::asio::io_service io_service; tcp::endpoint endpoint(address::from_string("127.0.0.1"), 6661); for (int i = 0; i < 5000; ++i) { client_ptr new_session(new client(io_service, endpoint)); new_session->start(); io_service.run(); std::cout << "第" << i << "" << std::endl; } std::string ss; std::cin >> ss; return 0; } ```