【问题标题】:Find machine IP adress from machine name [closed]从机器名称中查找机器 IP 地址 [关闭]
【发布时间】:2016-06-09 16:15:55
【问题描述】:

我知道远程机器名称,这台机器在同一个网络上。

如果我从命令提示符 ping 那台机器,我也会得到 IP 信息。

C:\Users\anikumar>ping neemqx01g

Pinging neemqx01g.efi.internal [10.210.98.194] with 32 bytes of data:
Reply from 10.210.98.194: bytes=32 time=2ms TTL=128
Reply from 10.210.98.194: bytes=32 time<1ms TTL=128
Reply from 10.210.98.194: bytes=32 time<1ms TTL=128
Reply from 10.210.98.194: bytes=32 time<1ms TTL=128

Ping statistics for 10.210.98.194:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 2ms, Average = 0ms

如何在wxWidgets或boost中以编程方式获得IP地址?

答案:

C 函数:

const wxString GetIPbyName(const wxString& name)
{
    struct hostent* pHostInfo;
    pHostInfo = gethostbyname(name.c_str());

    in_addr * address = (in_addr *)pHostInfo->h_addr;
    std::string ip_address = inet_ntoa(*address);

    return ip_address;
}

wxWidgets:

   wxIPV4address ipv4;
   ipv4.Hostname(m_currentServer);
   wxString m_currentServer = ipv4.IPAddress();

我不知道在多个活动网卡的情况下上述行为会如何。

【问题讨论】:

  • @YamMarcovic:OP 没有询问 URI,也没有询问 Boost.ASIO。
  • 我正在投票关闭作为资源请求。
  • @PreferenceBean 我认为纯粹的GUI框架中没有这样的组件;但是链接中的答案涵盖了使用可移植的 asio 解析主机名(正如您最终推荐自己的那样)。

标签: c++ boost wxwidgets


【解决方案1】:

我已经为你查看了文档,你不能

对于可移植的网络名称查找,您可以考虑使用通用的跨平台网络库,例如 Boost.ASIO

【讨论】:

  • 能否请您发布一个代码sn-p。
  • 嗯? “我怎样才能做到这一点?” -> “不,它没有。” ???
  • @immibis:是的,好的;)
【解决方案2】:

C 套接字库(几乎)是可移植的。您将有旧的gethostbyaddr 电话或较新的getaddrinfo 电话。

但您需要一些 #ifdef 才能使它们真正可移植,因为:

  • 标头在 Posix 系统上与 netdb.h(以及可选的 sys/types.h 和 sys/socket.h)与 Windows 上的 Ws2tcpip.hwinsock2.h 不同
  • 在 Windows 上,您需要调用 WSAStartup 来初始化 winsock。

【讨论】:

  • 这就是为什么我们让经过良好测试、广泛使用的库为我们做这件事。
猜你喜欢
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
相关资源
最近更新 更多