最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Get port used by a TCP- and SSL-based websocket::stream from Boost.Beast - Stack Overflow

programmeradmin9浏览0评论

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.

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.

Share Improve this question edited Jan 17 at 15:55 JaMiT 17.1k5 gold badges17 silver badges39 bronze badges asked Jan 17 at 15:18 Paul GrinbergPaul Grinberg 1,4773 gold badges18 silver badges46 bronze badges 2
  • You linked to the documentation for Boost 1.68. Is that the version you are using? (There was a change in 1.70, hence the version is relevant.) – JaMiT Commented Jan 17 at 16:01
  • Ah - that could very well be it. I'm using boost 1.74 – Paul Grinberg Commented Jan 17 at 17:08
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论