Resolve usage and Endpoint Analysis)

Source: Internet
Author: User

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.
Convert the IP address in string format, such as 192.168.0.200 or the Host Name HTTP: // localhost, and port "8080", to the internal socket representation format, so that we can directly use the string format during application, and there is no need to worry about the byte sequence conversion of the socket. Example: boost: ASIO: io_service;

 
TCP: resolver (io_service );

 
TCP: resolver: Query query ("localhost", "9000 ");

It should also be noted that boost: ASIO uses the endpoint representation for both parties (server and client), soAddress,PortThe IP addresses and ports are respectively encapsulated. 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:

TCP: resolver: iterator endpoint_iterator = resolver. Resolve (query );

TCP: resolver: iterator end;

Boost: System: error_code error = boost: ASIO: Error: host_not_found;

TCP: endpoint;

// If (error)

// Cout <"error is true! \ N ";

While (error & endpoint_iterator! = End)

{

 
Endpoint = * endpoint_iterator;

Socket. Close ();

Socket. Connect (endpoint, error );

Endpoint_iterator ++;

}

After obtaining the endpoint, let's just say that endpoint. Address (). to_string () can return the IP address in string format, short endpoint.Port() Return port.

In fact, the endpoint can be constructed by itself, and the method is also very simple,

TCP: endpoint (TCP: V4 (), (unsigned short) 9000) This is the server-side usage, TCP: V4 () directly returns its own address, if the IP 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 (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;

 
TCP: resolver (my_io_service );

 

TCP: resolver: Query query ("www.google.com", "HTTP ");

TCP: resolver: iterator iter = resolver. Resolve (query );

TCP: resolver: iterator end; // End marker.

While (ITER! = End)

{

TCP: endpoint = * ITER ++;

STD: cout <endpoint <STD: Endl;

}

In this way, we can find a new purpose. Through resolver iteration, We can get multiple node endpoints. For example, Google has several IP addresses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.