用Asio实现同步echo服务器
使用Asio实现同步echo服务器
服务端
sync_server.h
copy#ifndef ASYNC_SERVER_H #define ASYNC_SERVER_H #include <boost/asio/ip/tcp.hpp> #include <memory> #include <set> #include <thread> namespace MS { typedef std::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr; class AsyncServer { public: AsyncServer(unsigned short); ~AsyncServer() {} void listen(); void session(socket_ptr); private: unsigned short _port; boost::asio::ip::tcp::acceptor _acceptor; std::set<std::shared_ptr<std::thread>> _connect_thread; private: void read_message(); void send_message(); }; } #endif // ! ASYNC_SERVER_H
sync_client.cpp
copy#include <iostream> #include <string> #include <boost/asio/write.hpp> #include <boost/asio/read.hpp> #include "async_server.h" boost::asio::io_context IOC; namespace MS { using namespace std; using namespace boost::asio; #define MAX_SZIE 1024 AsyncServer::AsyncServer(unsigned short port) :_port{port}, _acceptor{IOC, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), _port)} { std::cout << "the server initing." << std::endl; this->listen(); std::cout << "the server is running." << std::endl; } void AsyncServer::listen() { while(1) { std::cout << "listen the connection...\n"; socket_ptr sock_{new ip::tcp::socket(IOC)}; this->_acceptor.accept(*sock_); std::cout << "new connection from " << sock_->remote_endpoint().address() << std::endl; char data[MAX_SZIE]; boost::system::error_code ec; memset(data, '\0', MAX_SZIE); sock_->async_read_some(boost::asio::buffer(data, MAX_SZIE), ec); if (ec.value() != 0) { std::cerr << "the async read is falie: " << ec.value() << " message: " << ec.message() << std::endl; } cout.write(data, MAX_SZIE); sock_->async_write_some(boost::asio::buffer(data, MAX_SZIE),ec); if (ec.value() != 0) { std::cerr << "the async send is falie: " << ec.value() << " message: " << ec.message() << std::endl; } //std::thread session_thread(MS::AsyncServer::session, this, sock_); /*std::thread session_thread([&]() { this->session(sock_); });*/ //session_thread.join(); //delete session_thread; } } void AsyncServer::session(socket_ptr socket) { for(;;){ try{ char data[MAX_SZIE]; memset(data, '\0', MAX_SZIE); boost::system::error_code error; size_t send_length = socket->read_some(boost::asio::buffer(data, MAX_SZIE), error); if(error.value() != 0){ cerr << "read message error,code is " << error.value() << ", message is " << error.message() << endl; return; } cout << "write message's length is : " << send_length << endl; cout.write(data, MAX_SZIE); socket->write_some(boost::asio::buffer(data), error); if(error.value() != 0){ cerr << "write message error: " << error.message() << endl; } } catch(...){ cout << "Client disconnected" << endl; } } } void AsyncServer::send_message() { } void AsyncServer::read_message() { } } int main() { try{ IOC.run(); MS::AsyncServer server(10086); } catch(std::exception &e){ std::cerr << "Exception: " << e.what() << std::endl; } }
客户端
sync_client.h
copy#pragma once #include <iostream> #include <boost/asio/ip/tcp.hpp> namespace MC { class Client { public: Client(const std::string&, unsigned short); void connect_to_server(); void runClient(); void send_message(); void receive_message(); private: boost::asio::ip::tcp::socket _sock; boost::asio::ip::tcp::endpoint _remote_endpoint; }; }
sync_client.cpp
copy #include <iostream> #include <boost/asio/write.hpp> #include <boost/asio/read.hpp> #include "async_client.h" static boost::asio::io_context IOC; namespace MC { using namespace std; using namespace boost::asio::ip; #define MAX_LENGTH 1024 Client::Client(const std::string& host, unsigned short port) :_sock{IOC}, _remote_endpoint{ address::from_string(host), port } { this->connect_to_server(); this->runClient(); } // connect to the server by host and port void Client::connect_to_server() { try { boost::system::error_code ec = boost::asio::error::host_not_found; _sock.connect(_remote_endpoint, ec); if(ec.value() != 0) { std::cout << "connect failed, code is " << ec.value() << " error msg is " << ec.message() << std::endl; return; } std::cout << "connect success!" << std::endl; }catch(...) { std::cout << "connect failed! system error." << std::endl; } } // run the client void Client::runClient(){ while(true) { this->send_message(); this->receive_message(); } } // send message to server void Client::send_message() { try { std::string msg; std::cout << "Enter message: "; std::getline(std::cin, msg); boost::asio::write(_sock, boost::asio::buffer(msg)); }catch(...) { std::cout << "send message failed! system error." << std::endl; } } void Client::receive_message() { try { char reply[MAX_LENGTH]; size_t reply_length = boost::asio::read(_sock, boost::asio::buffer(reply, MAX_LENGTH)); std::cout << "Reply is: "; std::cout.write(reply, reply_length); std::cout << "\n"; }catch(...) { std::cout << "receive message failed! system error." << std::endl; } } } int main() { MC::Client client("127.0.0.1", 10086); // client.runClient(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步