質問

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());
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top