Rel*_*lla 11 c++ ip tcp boost-asio
我有一个使用boost asio的TCP服务器.我接受了套接字连接.如何获取IP,我的服务器正在与之通信的机器端口?
顺便说一句:是否有可能获得有关连接服务器用户看到我的server4机器的IP的信息?
Ral*_*alf 25
您可以像这样获得IP和端口:
std::string sClientIp = socket().remote_endpoint().address().to_string();
unsigned short uiClientPort = socket().remote_endpoint().port();
Run Code Online (Sandbox Code Playgroud)
如果您在remote_endpoint中收到错误文件描述符错误,您仍然可以参考下面的链接。
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/basic_socket_acceptor/accept.html
“接受新连接并获取对等点的端点。” 部分会对你有帮助。
您可以使用如下
tcp::acceptor::endpoint_type end_type;
acceptor.accept(*stream.rdbuf(), end_type);
std::string sClientIp = end_type.address().to_string();
Run Code Online (Sandbox Code Playgroud)