【问题标题】:C++ Windows function call that get local hostname and IP address获取本地主机名和 IP 地址的 C++ Windows 函数调用
【发布时间】:2011-02-25 09:37:35
【问题描述】:

是否有可以获取主机名和 IP 地址的内置 windows C++ 函数调用?谢谢。

【问题讨论】:

    标签: c++ winapi ip-address hostname


    【解决方案1】:

    这是一个多平台解决方案... Windows、Linux 和 MacOSX。 可以获取ip地址、端口、sockaddr_in、端口。

    BOOL GetMyHostName(LPSTR pszBuffer, UINT nLen)
    {
        BOOL ret;
    
        ret = FALSE;
    
        if (pszBuffer && nLen)
        {
            if ( gethostname(pszBuffer, nLen) == 0 )
                ret = TRUE;
            else
                *pszBuffer = '\0';
        }
    
        return ret;
    }
    
    
    ULONG GetPeerName(SOCKET _clientSock, LPSTR _pIPStr, UINT _IPMaxLen, int *_pport)
    {
        struct sockaddr_in sin;
        unsigned long ipaddr;
    
    
        ipaddr = INADDR_NONE;
    
        if (_pIPStr && _IPMaxLen)
            *_pIPStr = '\0';
    
        if (_clientSock!=INVALID_SOCKET)
        {
            #if defined(_WIN32)
            int  locallen;
            #else
            UINT locallen;
            #endif
    
            locallen = sizeof(struct sockaddr_in);
    
            memset(&sin, '\0', locallen);
    
            if (getpeername(_clientSock, (struct sockaddr *) &sin, &locallen) == 0)
            {
                ipaddr = GetSinIP(&sin, _pIPStr, _IPMaxLen);
    
                if (_pport)
                    *_pport = GetSinPort(&sin);
            }
        }
    
        return ipaddr;
    }
    
    
    ULONG GetSinIP(struct sockaddr_in *_psin, LPSTR pIPStr, UINT IPMaxLen)
    {
        unsigned long ipaddr;
    
        ipaddr = INADDR_NONE;
    
        if (pIPStr && IPMaxLen)
            *pIPStr = '\0';
    
        if ( _psin )
        {
            #if defined(_WIN32)
            ipaddr = _psin->sin_addr.S_un.S_addr;
            #else
            ipaddr = _psin->sin_addr.s_addr;
            #endif
    
            if (pIPStr && IPMaxLen)
            {
                char  *pIP;
                struct in_addr in;
    
                #if defined(_WIN32)
                in.S_un.S_addr = ipaddr;
                #else
                in.s_addr = ipaddr;
                #endif
    
                pIP = inet_ntoa(in);
    
                if (pIP && strlen(pIP) < IPMaxLen)
                    strcpy(pIPStr, pIP);
            }
        }
    
        return ipaddr;
    }
    
    
    int GetSinPort(struct sockaddr_in *_psin)
    {
        int port;
    
        port = 0;
    
        if ( _psin )
            port = _Xntohs(_psin->sin_port);
    
        return port;
    }
    

    【讨论】:

    • 如何测试你的GetSinIP函数?
    • 请注意,gethostname() 返回的主机名不一定一定是任何主机接口的域名解析返回的名称!但是,如果以这种方式配置,它可能是相同的名称。
    • 缺少 adjust_str() 的实现我猜它会将数字 IP 转换为字符串?如果您添加它会很棒(尽管是 8 年前)
    • adjust_str() 只是一个复制字符串的安全函数。我已经在上面的代码中通过 strcpy() 对其进行了更改。
    【解决方案2】:

    要获取主机名,您可以使用:gethostname 或异步方法 WSAAsyncGetHostByName

    要获取地址信息,您可以使用:getaddrinfo或unicode版本GetAddrInfoW

    您可以使用 Win32 API 获取有关计算机名称(如域)的更多信息:GetComputerNameEx

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2014-06-01
      • 2018-01-02
      • 2018-08-26
      相关资源
      最近更新 更多