boost::asio가 작성한 비동기 서버와 클라이언트

클라이언트
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using boost::asio::ip::tcp;
using boost::asio::ip::address;

class client : public boost::enable_shared_from_this {
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!
"); 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,"
", 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_ptr; int main(int argc, char* argv[]) { boost::asio::io_service io_service; tcp::endpoint endpoint(address::from_string("127.0.0.1"), 1000); client_ptr new_session(new client(io_service, endpoint)); new_session->start(); io_service.run(); std::string ss; std::cin >> ss; return 0; }

서버
#include 
#include 
#include 
#include 
#include 

using boost::asio::ip::tcp;
using boost::asio::ip::address;

class session : public boost::enable_shared_from_this {
public:
	session(boost::asio::io_service &io_service) : socket_(io_service)
	{
	}

	void start() {
		static tcp::no_delay option(true);
		socket_.set_option(option);

		boost::asio::async_read_until(socket_,
			sbuf_,
			"
", 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_, "
", boost::bind(&session::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) { boost::asio::async_write(socket_, sbuf_, boost::bind(&session::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } private: tcp::socket socket_; boost::asio::streambuf sbuf_; }; typedef boost::shared_ptr session_ptr; class server { public: server(boost::asio::io_service &io_service, tcp::endpoint &endpoint) : io_service_(io_service), acceptor_(io_service, endpoint) { 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) { return; } new_session->start(); 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: boost::asio::io_service &io_service_; tcp::acceptor acceptor_; }; int main(int argc, char* argv[]) { boost::asio::io_service io_service; tcp::endpoint endpoint(tcp::v4(), 1000); server s(io_service, endpoint); s.run(); return 0; }

좋은 웹페이지 즐겨찾기