📜 ⬆️ ⬇️

"Boost.Asio C ++ Network Programming". Chapter 2: Boost.Asio Basics Part 1

Hello!
I continue to translate the book of John Torjo "Boost.Asio C ++ Network Programming". The second chapter turned out to be big, so I will break it into two parts. In this part we will talk about the basics of Boost.Asio, and in the second part we will discuss asynchronous programming.

Content:


In this chapter, we will look at what you need to know using Boost.Asio. We delve into asynchronous programming, which is much more complicated than synchronous and much more interesting.
')

Network API


This section shows what you need to know in order to write a network application using Boost.Asio.

Namespaces Boost.Asio

Everything in Boost.Asio is in the namespace boost::asio or its subspace, consider them:


IP addresses

To work with IP addresses, Boost. Asio provides the classes ip::address, ip::address_v4 and ip::address_v6 .
They provide many features. Here are the most important ones:

Most likely you will most often use the ip::address::from_string :

 ip::address addr = ip::address::from_string("127.0.0.1"); 

If you need to connect to the hostname, read on. The following code will not work:

 // throws an exception ip::address addr = ip::address::from_string("www.yahoo.com"); 


Endpoints

The endpoint is the address of the connection along with the port. Each type of socket has its own endpoint class, for example, ip::tcp::endpoint, ip::udp::endpoint , and ip::icmp::endpoint .
If you want to connect to localhost on port 80, then you need to write the following:

 ip::tcp::endpoint ep( ip::address::from_string("127.0.0.1"), 80); 

You can create an endpoint in three ways:

Here are some examples:

 ip::tcp::endpoint ep1; ip::tcp::endpoint ep2(ip::tcp::v4(), 80); ip::tcp::endpoint ep3( ip::address::from_string("127.0.0.1), 80); 

If you want to connect to a host (not an IP address), then you need to do the following:

  // outputs "87.248.122.122" io_service service; ip::tcp::resolver resolver(service); ip::tcp::resolver::query query("www.yahoo.com", "80"); ip::tcp::resolver::iterator iter = resolver.resolve( query); ip::tcp::endpoint ep = *iter; std::cout << ep.address().to_string() << std::endl; 

You can replace tcp with the type of socket you need. First, create a request with the name you want to connect to, this can be accomplished using the function resolve() . If successful, at least one entry will be returned.
After receiving the end point, you can get from it the address, port and IP protocol (v4 or v6):

 std::cout << ep.address().to_string() << ":" << ep.port() << "/" << ep.protocol() << std::endl; 


Sockets

Boost.Asio includes three types of socket classes: ip::tcp, ip::udp , and ip::icmp , and, of course, it expands. You can create your own socket class, although it is rather difficult. In case you still decide to do this, look at boost/ asio/ip/tcp.hpp , boost/asio/ip/udp.hpp , and boost/asio/ip/icmp.hpp . All of them are pretty small classes with internal typedef keywords.
You can think of the classes ip::tcp, ip::udp, ip::icmp as placeholders; they allow easy access to other classes / functions, which are defined as follows:

The socket class creates the corresponding socket. You always pass an instance of io_service to the constructor:

 io_service service; ip::udp::socket sock(service) sock.set_option(ip::udp::socket::reuse_address(true)); 

Each socket name has a typedef :

Source: https://habr.com/ru/post/193038/


All Articles