使用Boost ASIO查找服务器IP
Find server IP using Boost Asio
我正在使用boost asio库在C 上编写客户端。我想获取服务器IP的字符串表示形式,以显示在我的客户端日志中。有人知道该怎么做吗?
就获得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中看到过这样的功能。
std::string s = boost::lexical_cast<std::string>(socket.remote_endpoint());
这应该可以解决问题!
您可能想做主机查找:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/range/iterator_range.hpp>
using boost::asio::ip::tcp;
int main() {
char name[64], domain[64];
if (::gethostname(name, 64)) ::perror("gethostname failed");
if (::getdomainname(domain, 64)) ::perror("getdomainname failed");
boost::asio::io_service ios;
tcp::resolver res(ios);
std::string name_s(name), domain_s(domain);
for (auto match : boost::make_iterator_range(res.resolve({name_s+"."+domain_s, "0"}), {})) {
std::cout << name << " -> " << match.endpoint().address() << "n";
}
}
可能会打印
之类的东西desktop.fritz.box -> 192.168.182.20
相关文章:
- 我正在开发服务器,ip作为参数传递不起作用
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- C++ Winsock2 客户端未通过远程 IP 连接到服务器
- 将静态 IP 分配给我的 ESP32 服务器后不再响应
- UDP客户端/服务器程序在不同的IP和端口上通信超过2个插座
- 服务器和客户端都在PC上运行.客户端可以使用PC的IP连接到服务器
- 如何在 TCP/IP 服务器中中断 accept()?
- boost::asio::ip::tcp::iostream,先启动客户端,然后等待服务器
- 使用Boost ASIO查找服务器IP
- 可视化如何使用C++程序获取DHCPV6服务器IP.我尝试使用DHCPv6RequestParams,但总是收到错误代码
- 如果我们只知道IP,如何知道服务器是否在线
- C++客户端到Java服务器(TCP/IP)
- 查找 DNS 服务器和网关的 IP 和 MAC 地址
- 在 ipv6 链接范围单播 udp 客户端-服务器中应用 boost::asio::ip::udp,有时有效,但有时无效
- 如何知道 IP 地址是否在 Linux 服务器上是本地的
- 在 Boost ASIO 中,如何设置源 IP 地址以模拟其他服务器的 IP 地址?
- 无法在 gSOAP 服务器中获取 SOAP 客户端的 IP 地址
- 通过其 IP 地址检测服务器是否在本地运行
- 使用 IOCP 的 TCP/IP 服务器.接收缓冲区中偶尔的数据损坏
- 提升连接后从TCP服务器访问TCP客户端IP地址+端口