I have an instace of
boost::beast::websocket::stream<
boost::beast::ssl_stream<
boost::beast::tcp_stream>>
and I'd like to get the port number used by the underlying socket for this stream. I can't seem to figure out the magic for this.
If I'm reading the documentation correctly, boost::beast::stream
is support to have lowest_layer()
function, but my compiler says otherwise. And if I start going down the next_layer
rabbit hole, I still can't figure out how to get the port number.
Beast has a free function that helps getting the lowest layer:
boost::asio::ssl::context ctx{boost::asio::ssl::context::tlsv12_client};
boost::beast::websocket::stream<boost::beast::ssl_stream<boost::beast::tcp_stream>> x{ex, ctx};
auto& stream = get_lowest_layer(x);
Now, since you chose tcp_stream
as the lowest layer, you can get the underlying socket:
auto& socket = stream.socket();
And that gives you the endpoints, eg.:
auto ep = socket.remote_endpoint();
std::cout << ep << "\n";
Or if indeed you want the separate details:
std::cout << "Host: " << ep.address() << "\n";
std::cout << "Port: " << ep.port() << "\n";
See it all Live On Coliru