1. 程式人生 > >boost::asio編寫的非同步伺服器和客戶端

boost::asio編寫的非同步伺服器和客戶端

客戶端

#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"), 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 <string.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

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);

		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));
	}

	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> 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;
}