请问什么函数可以根据ip地址得到远程主机名?
我找了半天都没找到!

解决方案 »

  1.   

    struct HOSTENT FAR * gethostbyaddr(
      const char FAR *addr,
      int len,
      int type
    );
      

  2.   

    char szhostname[128];
         CString str;
    if( gethostname(szhostname, 128) == 0 )
    {
    // get host adresses
    struct hostent * phost;
    int i;
     
    phost = gethostbyname(szhostname);
            m_hostname=szhostname;
    //for( i = 0; phost!= NULL && phost->h_addr_list[i]!= NULL; i++ )
      //{
    i=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[i])[j]);
    str += addr;
      }
       // str now contains one local ip address - do whatever you want to do with it (probably add it to a list)
      //}
    }
        m_ipaddress=str;
    UpdateData(FALSE);