可否通过Windows局域网中的主机名获得主机的IP地址,怎样操作???期待你的高见,高分奉送。

解决方案 »

  1.   

    gethostbyaddr和gethostbyname
    我有例子,告诉我你的邮箱。
      

  2.   

    从主机名得到IP地址
    int CGetIPDlg::GetIPAddress(const CString& sHostName, CString& sIPAddress)
    {
    struct hostent FAR *lpHostEnt = gethostbyname (sHostName);if (lpHostEnt == NULL) {
    // An error occurred. 
    sIPAddress = _T("");
    return WSAGetLastError();
    }LPSTR lpAddr = lpHostEnt->h_addr_list[0];
    if (lpAddr) {
    struct in_addr  inAddr;
    memmove (&inAddr, lpAddr, 4);
    sIPAddress = inet_ntoa (inAddr);
    if (sIPAddress.IsEmpty())
    sIPAddress = _T("Not available");
    } return 0;
    }
      

  3.   

    //host 为主机名
    char *host;
    struct hostent * hostinfohostinfo = gethostbyname(host);
    if(!hostinfo) return;hostinfo->h_aliases; //为主机IP
      

  4.   

    在命令行下:
    ping 主机名
      

  5.   

    CString m_user; //主機名
    hostent *Me_Info=NULL;
    Me_Info=gethostbyname(m_user);
    if(Me_Info!=NULL)
        inet_ntoa(*(struct in_addr*)Me_Info->h_addr_list[0]); //IP Address