boost :: asio :: ip :: tcp :: socketのIPアドレスを取得する方法は?
-
03-07-2019 - |
質問
Boost ASIOライブラリを使用してC ++でサーバーを記述しています。サーバーのログに表示されるクライアントIPの文字列表現を取得したいです。誰もそれを行う方法を知っていますか?
解決
ソケットには、リモートエンドポイントを取得する機能があります。この(長い)コマンドチェーンを試してみましょう。リモートエンドIPアドレスの文字列表現を取得する必要があります。
asio::ip::tcp::socket socket(io_service);
// Do all your accepting and other stuff here.
asio::ip::tcp::endpoint remote_ep = socket.remote_endpoint();
asio::ip::address remote_ad = remote_ep.address();
std::string s = remote_ad.to_string();
またはワンライナーバージョン:
asio::ip::tcp::socket socket(io_service);
// Do all your accepting and other stuff here.
std::string s = socket.remote_endpoint().address().to_string();
他のヒント
または、 boost :: lexical_cast
:
#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>(socket.remote_endpoint());
所属していません StackOverflow