Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Establishing Connections

Connections are established by invoking functions directly on the next layer object. For example, to make an outgoing connection using a standard TCP/IP socket:

std::string const host = "";
boost::asio::ip::tcp::resolver r{ioc};
stream<boost::asio::ip::tcp::socket> ws{ioc};
auto const results = r.resolve(host, "ws");
boost::asio::connect(ws.next_layer(), results.begin(), results.end());

Similarly, to accept an incoming connection using a standard TCP/IP socket, pass the next layer object to the acceptor:

boost::asio::ip::tcp::acceptor acceptor{ioc};
stream<boost::asio::ip::tcp::socket> ws{acceptor.get_executor().context()};

When using SSL, which itself wraps a next layer object that is usually a TCP/IP socket, multiple calls to retrieve the next layer may be required. In this example, the websocket stream wraps the SSL stream which wraps the TCP/IP socket:

boost::asio::ip::tcp::endpoint ep;
boost::asio::ssl::context ctx{boost::asio::ssl::context::sslv23};
stream<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> ws{ioc, ctx};

// connect the underlying TCP/IP socket

// perform SSL handshake

// perform WebSocket handshake
ws.handshake("localhost", "/");
[Note] Note

Examples use synchronous interfaces for clarity of exposition; signatures for asynchronous operations are also provided.