以下代码是在boost::asio的Tutorial的Daytime.3 - An asynchronous TCP daytime server示例上加了几行 // // server.cpp // ~~~~~~~~~~ // // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // //#include <ctime> #include <iostream> #include <string> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; //std::string make_daytime_string() //{ // using namespace std; // For time_t, time and ctime; // time_t now = time(0); // return ctime(&now); //} class tcp_connection : public boost::enable_shared_from_this<tcp_connection> { public: typedef boost::shared_ptr<tcp_connection> pointer; static pointer create(boost::asio::io_service& io_service) { return pointer(new tcp_connection(io_service)); } tcp::socket& socket() { return socket_; } void start() { socket_.async_read_some(boost::asio::buffer(buffer), boost::bind(&tcp_connection::OnRead, this->shared_from_this(),_1,_2) ); //1.这里没有用boost::asio::async_read的原因是: //boost::asio::async_read会读完这次的数据流,而不是读一个数据就立刻返回一个,相当于多个async_read_some()操作。它的操作定义是: //This operation is implemented in terms of zero or more calls to the stream's // * async_read_some function, and is known as a <em>composed operation</em>. The // * program must ensure that the stream performs no other read operations (such // * as async_read, the stream's async_read_some function, or any other composed // * operations that perform reads) until this operation completes. // std::cout<<"read over!"<<std::endl; //2.注意此处shared_ptr的使用,保证直到指针不指向该对象时,该对象才被销毁。 //如果不使用,则会报错。因为不保证在执行回调函数的时候tcp_connection对象还存在 //3.注意此时要获得传回数据的长度,否则write操作会发送所有buffer里的数据 } void OnRead(const boost::system::error_code& error,std::size_t bytes_transfered) { if(!error) { std::cout<<"bytes transfered are "<<bytes_transfered<<std::endl; boost::asio::async_write(socket_,boost::asio::buffer(buffer,bytes_transfered), boost::bind(&tcp_connection::OnWrite,this->shared_from_this()) //这里没有使用socket_.async_write_some的原因是async_write_some 不保证发送所有的数据 ); } void OnWrite() { start(); } private: tcp_connection(boost::asio::io_service& io_service) : socket_(io_service) { } //void handle_write(const boost::system::error_code& /*error*/, // size_t /*bytes_transferred*/) //{ //} tcp::socket socket_; std::string message_; char buffer[1024]; }; class tcp_server { public: tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13)) { start_accept(); } private: void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.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_; }; int main() { try { boost::asio::io_service io_service; tcp_server server(io_service); io_service.run(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }