//服务端#include #include #include #include using boost::asio::ip::tcp;using namespace std;class session{public:session(boost::asio::io_service& io_service): socket_(io_service){//to initional the data_memset(data_, 0, sizeof(data_));}tcp::socket& socket(){return socket_;}void start(){cout << "start async_read_some ......socket=" << &socket_ << endl;//async_read_somesocket_.async_read_some(boost::asio::buffer(data_, max_length),boost::bind(&session::handle_read, this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));// async_read(socket_, boost::asio::buffer(data_, max_length),// boost::bind(&session::handle_read, this,// boost::asio::placeholders::error,// boost::asio::placeholders::bytes_transferred));}private:void handle_read(const boost::system::error_code& error,size_t bytes_transferred){cout << "handle_read......" << "socket=" << &socket_ << " buffer=" << data_ << " length=" << bytes_transferred << endl;if (!error){boost::asio::async_write(socket_,boost::asio::buffer(data_, bytes_transferred),boost::bind(&session::handle_write, this, //notice: this handle_write function is not static functionboost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));}else{cout << "handle_read delete session......" << endl;delete this;}}void handle_write(const boost::system::error_code& error, size_t bytes_transferred){// cout << "handle write......" << data_ << " length:" << bytes_transferred << endl;if (!error){socket_.async_read_some(boost::asio::buffer(data_, max_length),boost::bind(&session::handle_read, this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));}else{cout << "handle_write delete session......" << endl;delete this;}}tcp::socket socket_;enum { max_length = 1024 };char data_[max_length];};class server{public:server(boost::asio::io_service& io_service, short port, int num): io_service_(io_service), number(num),acceptor_(io_service, tcp::endpoint(tcp::v4(), port)){start_accept();}private:void start_accept(){cout << "start new accept......" << endl;session* 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* new_session,const boost::system::error_code& error){char cznum[1024] = {0};sprintf(cznum, "number=%d\n", number);cout << "get remote address:" << new_session->socket().remote_endpoint().address() << " number=" << number << endl;if (!error){new_session->start();}else{delete new_session;}start_accept();}boost::asio::io_service& io_service_;tcp::acceptor acceptor_;int number;};int main(int argc, char* argv[]){try{cout << "start asio server......" << endl;boost::asio::io_service io_service;using namespace std; // For atoi.server s(io_service, 1000, 1);io_service.run();}catch (std::exception& e){std::cerr << "Exception: " << e.what() << "\n";}return 0;}//客户端#include #include using namespace std;int main(int argc, char* argv[]){using namespace boost::asio;// 所有asio类都需要io_service对象io_service iosev;// socket对象ip::tcp::socket socket(iosev);// 连接端点,这里使用了本机连接,可以修改IP地址测试远程连接ip::tcp::endpoint ep(ip::address_v4::from_string("192.168.106.92"), 1000);// 连接服务器boost::system::error_code ec;socket.connect(ep,ec);// 如果出错,打印出错信息if(ec){std::cout << "Exception:" << boost::system::system_error(ec).what() << std::endl;getchar();return -1;}size_t len = socket.write_some(buffer("123456"), ec);cout << len << endl;while (true){// 接收数据char buf[100];memset(buf, 0, sizeof(buf));size_t len=socket.read_some(buffer(buf), ec);if (strlen(buf) == 0){Sleep(100);continue;}std::cout.write(buf, len);if(ec){std::cout << "1:" <<boost::system::system_error(ec).what() << std::endl;// getchar();}Sleep(100);}getchar();return 0;}
asio异步通信
最新推荐文章于 2022-03-23 09:25:57 发布