문제

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();

또는 1 라이너 버전 :

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