使用那个函数可以获取计算机的IP地址

解决方案 »

  1.   

    int gethostname(
      char FAR *name,  
      int namelen       
    );
      

  2.   

    int gethostname(
      char FAR *name,  
      int namelen       
    );获取本机机器名,然后使用
    INT GetAddressByName(
      DWORD dwNameSpace,     // name space to query for service address 
                             // information
      LPGUID lpServiceType,  // the type of the service
      LPTSTR lpServiceName,  // the name of the service
      LPINT lpiProtocols,    // points to array of protocol identifiers
      DWORD dwResolution,    // set of bit flags that specify aspects of 
                             // name resolution
      LPSERVICE_ASYNC_INFO lpServiceAsyncInfo,
                             // reserved for future use, must be NULL
      LPVOID lpCsaddrBuffer, // points to buffer to receive address 
                             // information
      LPDWORD lpdwBufferLength,  // points to variable with address 
                                 // buffer size information
      LPTSTR lpAliasBuffer,  // points to buffer to receive alias 
                             // information
      LPDWORD lpdwAliasBufferLength 
                             // points to variable with alias buffer 
                             // size information
    );可以获取IP地址!
    例子:
    void CNetBaseDlg::GetHostName()
    {
    char * name = new char[255];
    gethostname(name, 255); hostent * pHostent = gethostbyname(name);
    hostent & he = *pHostent;
    sockaddr_in sa;
    memcpy(&sa.sin_addr.s_addr, he.h_addr_list[0],he.h_length);
    m_strIP = CString(inet_ntoa(sa.sin_addr));//ip地址
    }
      

  3.   

    char *  FuncGetLocalIpAddress() 
    {
    char szHostName[128];
    WSADATA wsaData;
    struct hostent *pHost = NULL; memset(szHostName, 0, sizeof(szHostName)); WSAStartup(0x0101, &wsaData);
    gethostname(szHostName, sizeof(szHostName));
    pHost = gethostbyname(szHostName);
    LPCSTR lpIP = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[0]); WSACleanup();

    return lpIP;
    }
      

  4.   

    //取出本机的名称和ip地址
    WORD wVersionRequested;
    WSADATA wsaData;
    TCHAR name[255],*ip;
    PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD(1,1);

    if(WSAStartup(wVersionRequested,&wsaData)==0)
    {
      if(gethostname(name,sizeof(name))==0)
      {
        if((hostinfo=gethostbyname(name))!= NULL)
        {
    int nCount=0;
    while(hostinfo->h_addr_list[nCount])
    {
                ip=inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[nCount]);
       ++nCount;
    }
        }
      }
    }
    m_csLocalIP.Format("%s",ip);
      

  5.   

    添加两个编辑框,对应的变量分别为CString m_hostname,CString m_ipaddress;
    gethostname(char FAR *name,int namelen);
    struct hostent FAR *gethostbyname(const char FAR *name);
    void CXXX::GetIP()
    {
       char szhostname[128];
       CString str;//获得主机名
       if(gethostname(szhostname,128)==0)
       {
           struct hostent *phost;// 获得主机ip地址
           int occurred;
           phost = gethostbyname(szhostname);
           m_hostname = szhostname;
           occurred = 0;
           int j;
           int h_length = 4;
           for(j=0;j<h_length;j++)
          {   
            CString addr;
            if(j>0)
            str +=".";
            addr.Format("%u",(unsigned int)unsigned char*)phost->h_addr_list[occurred])[j]);
            str+=addr;
           }
        }
        m_ipaddress = str;
        UpdateData(FALSE);
    }