摘于boost官网的几个例子, 做了点小修改
同步方法——客户端
同步——服务端
异步——服务端
同步方法——客户端
void test_asio_synclient() { typedef boost::asio::io_service IoService; // 该命名空间下有几个常用类: accetpt, resolver, endpoint, socket typedef boost::asio::ip::tcp TCP; try { IoService ios; boost::system::error_code error; TCP::socket socket(ios); TCP::endpoint endpoint(boost::asio::ip::address_v4::from_string("127.0.0.1"), 10000); socket.connect(endpoint, error); // 这里要判断一下, 否则没有连上会通过. if (error) throw boost::system::system_error(error); while(true) { // boost::arraybuf; std::vector buf(128); size_t len = socket.read_some(boost::asio::buffer(buf), error); // 这是也要判断一下, 否则服务端运行断开, 这里会出现死循环. if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. // buf要注意控制大小。 socket.write_some(boost::asio::buffer(buf, len), error); } } catch (std::exception& e) { PRINT_DEBUG(e.what()); } }
同步——服务端
namespace { std::string make_daytime_string() { using namespace std; // For time_t, time and ctime; time_t now = time(0); return ctime(&now); } } void test_asio_synserver() { typedef boost::asio::io_service IoService; typedef boost::asio::ip::tcp TCP; try { IoService ios; /* *以下构造函数相当于以下步骤 * basic_socket_acceptoracceptor(io_service); * acceptor.open(tcp::v4()); * acceptor.bind(tcp::endpoint(13)); * acceptor.listen(0);//default */ /* tcp::endpoint(端点)由以下三个部分组成 1. IP地址(address, address_v4, address_v6) 2. 端口号 3. 协议版本 */ TCP::acceptor acceptor(ios, TCP::endpoint(TCP::v4(), 13)); for (;;) { TCP::socket socket(ios); acceptor.accept(socket); std::string message = make_daytime_string(); boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message), boost::asio::transfer_all(), ignored_error); PRINT_DEBUG(message); } } catch (std::exception& e) { std::cout << e.what() << std::endl; } }
异步——服务端
namespace { typedef boost::asio::io_service IoService; typedef boost::asio::ip::tcp TCP; std::string make_daytime_string() { using namespace std; time_t now = std::time(NULL); return ctime(&now); } class tcp_connection : public boost::enable_shared_from_this{ public: typedef boost::shared_ptr pointer; static pointer create(IoService& io_service) { return pointer(new tcp_connection(io_service)); } TCP::socket& socket() { return socket_; } void start() { message_ = make_daytime_string(); boost::asio::async_write( socket_, boost::asio::buffer(message_), boost::bind(&tcp_connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } private: tcp_connection(IoService& io_service) : socket_(io_service) { } void handle_write(const boost::system::error_code& /*error*/, size_t /*bytes_transferred*/) { PRINT_DEBUG("write data!!!"); } TCP::socket socket_; std::string message_; }; class tcp_server { public: tcp_server(IoService& io_service) : acceptor_(io_service, TCP::endpoint(TCP::v4(), 10000)) { start_accept(); } private: void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.get_io_service()); acceptor_.async_accept( new_connection->socket(), boost::bind(&tcp_server::handle_accept, this, new_connection, boost::asio::placeholders::error)); } void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error) { if (!error) { new_connection->start(); start_accept(); } } TCP::acceptor acceptor_; }; } // tcp_connection与tcp_server封装后 void test_asio_asynserver() { try { IoService io_service; tcp_server server(io_service); // 只有io_service类的run()方法运行之后回调对象才会被调用 io_service.run(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } }
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2302
- 用户1336
- 访客10970171
每日一句
Qingming Festival invites us to honor ancestors with quiet reflection and respect.
清明节邀请我们以静思与敬意祭奠祖先。
清明节邀请我们以静思与敬意祭奠祖先。
新会员