TCP: resolver is generally used in combination with TCP: resolver: Query. You can use the word "query" to obtain the corresponding information of the socket, generally, we care about the address and port of socket. through TCP: resolver, it is easy to set and query. It uses query to set the IP address in string format, such as 192.168.0.200 or the Host Name HTTP: // The localhost and port "8080" are converted into the internal representation format of the socket, so that we can directly use the string format during the application, and there is no need to worry about the byte sequence conversion of the socket. Example:
boost::asio::io_service io_service ;boost::asio::ip::tcp::resolver resolver(io_service);boost::asio::ip::tcp::resolver::query query("localhost", "9000");
It should also be noted that boost: ASIO uses the endpoint representation for both parties (server and client), so the address and port in the endpoint encapsulate the IP address and port respectively. It seems that Resolver and Endpoint are irrelevant, so TCP: resolver: iterator appears. It is the resolver iterator, which is actually the endpoint pointer, so you can do this:
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);boost::asio::ip::tcp::resolver::iterator end;boost::system::error_code error = boost::asio::error::host_not_found;boost::asio::ip::tcp::endpoint endpoint;while (error && endpoint_iterator != end){ endpoint = *endpoint_iterator ; socket.close(); socket.connect(endpoint, error); endpoint_iterator++ ;}
After you get the endpoint, you can simply say that endpoint. Address (). to_string () will be able to return the IP address in string format, and endpoint. Port () will return the port.
ActuallyThe endpoint can be constructed by itself., The method is also very simple, TCP: endpoint (TCP: V4 (), (unsigned short) 9000) This is the server side usage, TCP: V4 () directly return your address. If the address is used for the client, you need to set the IP address of the server as follows:
boost::system::error_code error = boost::asio::error::host_not_found;boost::asio::ip::address add;add.from_string("127.0.0.1");tcp::endpoint endpoint(add, short(9000));socket.connect(endpoint, error);
In this way, resolver is not used.
What's more amazing:
boost::asio::io_service ioservice ;boost::asio::io_service my_io_service ;boost::asio::ip::tcp::resolver resolver(my_io_service);boost::asio::ip::tcp::resolver::query query("www.google.com", "http");boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);boost::asio::ip::tcp::resolver::iterator end; // End marker.while (iter != end){ boost::asio::ip::tcp::endpoint endpoint = *iter++; std::cout << endpoint << std::endl;}
In this way, we can find a new purpose,Multiple node endpoints can be obtained through resolver IterationFor example, Google has several IP addresses.
The running result of the above example:
74.125.128.106:8074.125.128.147:8074.125.128.99:8074.125.128.103:8074.125.128.104:8074.125.128.105:80