This article uses a sample code to illustrate how to use the boost ASIO for synchronous communication.
#include <iostream> #include <boost/asio.hpp>using namespace std;using Boost::asio::ip::tcp; int main () { boost::asio::io_service IOSERVICE;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;I/O Event model encapsulation, such as win under the IOCP , Unix under the Poll, linux under the Epoll and so on tcp::socket socket (Ioservice); tcp:: endpoint endpoint; &NBSP;&NBSP;&NBSP;//&NBSP;TCP endpoint, including address and Port boost::system::error_code errcode; tcp::resolver resolver (ioservice); // address name Resolver tcp::resolver::iterator iter_endpoint; // reSolver can return multiple addresses at once, using iterators to access tcp::resolver::query query ("mail.163.com", "POP3"); // Host address Query condition class, the first parameter is the hostname, and the next one is the service name of the desired connection. // Specify the service name to enable the returned endpoint to include the host address, and also set the default port for the service (for example, POP3 default port is 110 // If you only need to query the host address, the service name can be an empty string (the Returned Endpoint.port () ==0) iter_endpoint = resolver.resolve (Query, errcode); // resolve the required service address if ( errcode.value () != 0) { cout<< "error code: " <<errcode.value () << ", " << "error message: " <<errcode.message () <<endl; return 1; } // Gets the target service endpoint address and port foundstored in endpoint instances) endpoint = iter_endpoint->endpoint (); cout<<endpoint.address () << ":" <<endpoint.port () <<endl; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;POP3 Command const char *cmd_user = "user myuser\n"; const char *cmd_pass = "pass mypass\n "; const char *cmd_list = " list\n "; const char *cmd_quit = "quit\n"; char result[256]; // server returns message Cache try { // connect the POP3 server and get the server welcome message. The // boost::asio::buffer function creates a cache wrapper object with an internal address pointing to the result variable socket.connect (endpoint); size_t len = socket.read_some (Boost::asio::buffer (result, 256)); if ( len > 0 ) { result[len] = '; ' cout<< "received: " <<result <<endl; } // Send user command, and receive reply socket.send (Boost::asio::buffer (Cmd_user, strlen (Cmd_user))); len = socket.read_some (Boost::asio::buffer (result, 256)); if ( len > 0 ) { result[len] = '; cout<< "received: " <<result<<endl; } // Send Password command, and receive reply socket.send (Boost::asio::buffer (Cmd_pass, strlen (Cmd_pass))); len = socket.read_some (Boost::asio::buffer (result, 256)); if ( len > 0 ) { result[len] = '; ' cout<< "received: " <<result<<endl; } // Send Exit command, Receive reply &nbsP; socket.send (Boost::asio::buffer (Cmd_quit, strlen (cmd_quit))); len = socket.read_some (Boost::asio::buffer (result, 256)); if ( len > 0 ) { result[len] = '; ' cout<< "received: " <<result<<endl; } socket.close ( ); } catch (const boost::system::system_error& e) { cout<< "connect error: " <<e.what () <<endl; } return 0;}
Boost ASIO learning note [1]-synchronous communication