zoukankan      html  css  js  c++  java
  • boost asio 网络通信 入门学习

    • 具体知识查看 《Boost程序库完全开放指南》 12.3.4 网络通信

    服务端代码

    #include <iostream>
    #include <boost/asio/ip/tcp.hpp>
    #include <boost/asio/io_service.hpp>
    #include <boost/asio/placeholders.hpp>
    #include <boost/bind.hpp>
    using namespace std;
    using namespace boost::asio;
    
    class server
    {
    	typedef server this_type;
    	typedef ip::tcp::endpoint endpoint_type;
    	typedef ip::tcp::acceptor acceptor_type;
    	typedef ip::tcp::socket socket_type;
    	typedef ip::address address_type;
    	typedef shared_ptr<socket_type> sock_ptr;
    
    public:
    	// 构造函数 初始化 acceptor 对象,并用accept函数 启动 异步服务
    	server() :m_acceptor(m_io, endpoint_type(ip::tcp::v4(), 6688))
    	{
    		accept();
    	}
    
    	void run()
    	{
    		m_io.run();
    	}
    
    	void accept_handler(const error_code& ec, sock_ptr sock)
    	{
    		if (ec) // 检测错误码
    		{
    			cout << "error:" << ec.message() << endl;
    			return;
    		}
    		cout << "client:";
    		cout << sock->remote_endpoint().address() << endl; // 输出连接的客户端信息
    
    		// 异步发送数据
    		sock->async_write_some(buffer("hello asio"), boost::bind(&this_type::write_handler, this, boost::asio::placeholders::error));
    
    		accept(); // 再次启动异步接受连接
    	}
    
    	void accept() // 启动异步服务
    	{
    		sock_ptr sock = std::make_shared<socket_type>(m_io); // 智能指针
    
    		// 异步监听服务
    		// 当TCP 连接发生时,accept_handler 将被调用,它使用 socket对象 发送数据
    		m_acceptor.async_accept(*sock, boost::bind(&this_type::accept_handler, this, boost::asio::placeholders::error, sock));;
    	}
    
    	void write_handler(const boost::system::error_code& ec)
    	{
    		cout << "send msg complete." << endl;
    	}
    
    private:
    	io_service m_io;
    	acceptor_type m_acceptor;
    };
    
    // 异步通信
    int main()
    {
    	try
    	{
    		cout << "server start." << endl;
    		server srv;
    		srv.run();
    	}
    	catch (std::exception& e)
    	{
    		cout << e.what() << endl;
    	}
    }
    
    
    // 同步通信
    // int main()
    // {
    // 	try
    // 	{
    // 		typedef ip::tcp::endpoint endpoint_type;
    // 		typedef ip::tcp::acceptor acceptor_type;
    // 		typedef ip::tcp::socket socket_type;
    // 		typedef ip::address address_type;
    //
    // 		cout << "server start." << endl;
    //
    // 		io_service io;
    //
    // 		acceptor_type acceptor(io, endpoint_type(ip::tcp::v4(), 6688)); // 创建 acceptor对象,ipv4,接收6688端口,开始监听
    //
    // 		cout << acceptor.local_endpoint().address() << endl;
    //
    // 		string cmd;
    // 		while (cin >> cmd)
    // 		{
    //
    // 			if (cmd == "send")
    // 			{
    // 				socket_type sock(io);
    // 				acceptor.accept(sock);
    //
    // 				cout << "client:";
    // 				cout << sock.remote_endpoint().address() << endl;
    // 				sock.send(buffer("hello asio"));
    // 			}
    // 			else if (cmd == "end")
    // 			{
    // 				break;
    // 			}
    // 			else
    // 			{
    // 				cout << "error cmd" << endl;
    // 			}
    // 		}
    // 	}
    // 	catch (std::exception& e)
    // 	{
    // 		cout << e.what() << endl;
    // 	}
    // }
    

    客户端代码

    //客户端代码
    
    #include <iostream>
    #include <boost/asio/ip/tcp.hpp>
    #include <boost/asio/io_service.hpp>
    #include <boost/asio/placeholders.hpp>
    #include <boost/bind.hpp>
    using namespace std;
    using namespace boost::asio;
    
    
    class client
    {
    	typedef client this_type;
    	typedef ip::tcp::endpoint endpoint_type;
    	typedef ip::address address_type;
    	typedef ip::tcp::socket socket_type;
    	typedef shared_ptr<socket_type> sock_ptr;
    	typedef vector<char> buffer_type;
    
    public:
    	client() :m_buf(100, 0), m_ep(address_type::from_string("127.0.0.1"), 6688)
    	{
    		start();
    	}
    
    	void run()
    	{
    		m_io.run();
    	}
    
    	void start()
    	{
    		sock_ptr sock = std::make_shared<socket_type>(m_io);
    		// 异步连接成功时, conn_handler 被调用,它在用buffer函数,将vector 作为接收数据的缓冲区,由async_read_some() 异步读取
    		sock->async_connect(m_ep, bind(&this_type::conn_handler, this, boost::asio::placeholders::error, sock));
    	}
    
    	void conn_handler(const error_code& ec, sock_ptr sock)
    	{
    		if (ec)
    		{
    			return;
    		}
    
    		cout << "receive from " << sock->remote_endpoint().address() << endl;
    
    		sock->async_read_some(buffer(m_buf), boost::bind(&client::read_handler, this, boost::asio::placeholders::error));
    	}
    
    	void read_handler(const error_code& ec)
    	{
    		if (ec)
    		{
    			cout << "error:" << ec.message() << endl;
    			return;
    		}
    		cout << &m_buf[0] << endl;
    	}
    
    private:
    	io_service m_io;
    	buffer_type m_buf;
    	endpoint_type m_ep;
    
    };
    
    // 异步通信
    int main()
    {
    	try
    	{
    		cout << "client start." << endl;
    		client cl;
    		cl.run();
    		getchar();
    	}
    	catch (std::exception& e)
    	{
    		cout << e.what() << endl;
    	}
    }
    
    
    // 同步通信
    // int main()
    // {
    // 	try
    // 	{
    // 		typedef ip::tcp::endpoint endpoint_type;
    // 		typedef ip::tcp::socket socket_type;
    // 		typedef ip::address address_type;
    //
    // 		cout << "client start." << endl;
    //
    // 		io_service io;
    //
    // 		socket_type sock(io); // 创建socket对象
    // 		endpoint_type ep(address_type::from_string("127.0.0.1"), 6688); // 创建连接端点 对方?
    // 		sock.connect(ep); // socket 连接到 端点
    // 		string cmd;
    //
    // 		cout << " sock.available() = " << sock.available() << endl; // 获取可读取的字节数
    //
    // 		vector<char> str(1000, 0); // 定义一个vector缓冲区
    //
    // 		sock.receive(buffer(str));
    //
    // 		cout << " receive from " << sock.remote_endpoint().address() << " content: " << &str[0] << endl;
    //
    //
    //
    // 	}
    // 	catch (std::exception& e)
    // 	{
    // 		cout << e.what() << endl;
    // 	}
    // }
    
  • 相关阅读:
    JAVA类加载机制
    redis 持久化的两种方式
    java动态代理(JDK和cglib)
    数据库事务的四大特性以及事务的隔离级别
    数据库范式
    Cookie/Session机制详解
    java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier)
    BIO与NIO、AIO的区别
    高性能Server---Reactor模型
    Netty---相关
  • 原文地址:https://www.cnblogs.com/q1076452761/p/15425747.html
Copyright © 2011-2022 走看看