c++ - 从机器名称中查找机器 IP 地址

标签 c++ boost wxwidgets

<分区>

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

如果我从命令提示符下 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();

我不知道上面在多个事件网卡的情况下会如何表现。

最佳答案

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

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

关于c++ - 从机器名称中查找机器 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35651872/

相关文章:

c++ - 如何查找进程是否挂起?

c++ - 在 visual studio : libboost_regex-vc120-mt-sgd-1_59. lib(instances.obj) 中使用内置的 boost 库时:错误 LNK2038:检测到不匹配

c++ - 定义 NDEBUG 时 boost 序列化链接器错误

c++ - 在Windows 10上使用wxWidgets(MinGW模式)编译源代码的问题

c++ - 在 wxFrame 上处理来自 wxTextCtrl 的事件 - C++/wxWidgets

c++ - Cocos2d-x 4.0 Lens3D和Waves3D动画

c++ - 命名空间范围内的 const 变量不是隐式静态的吗?

c++ - 乘以0优化

C++:无法使用 scoped_allocator_adaptor 传播 polymorphic_allocator

ubuntu - wxWidgets 如何使用代码块从 linux 交叉编译 Windows 应用程序?