最好带注解的要全文件.不只要主要的文件.我大体上原理都明白.好象总是有错误.
谢谢.

解决方案 »

  1.   

    给你一个函数吧,可以得到计算机名和本地IP地址:
    缓冲区szIp是调用者分配的,用来返回本地的IPbool GetLocHostIp(char *szIp)
    {
    char szHostName[128];   
              
    if(gethostname(szHostName, 128) == 0) //计算机名, 变量szHostName就是本机名  
    {    
    struct   hostent   *   pHost;   
    int   i;   
    pHost = gethostbyname(szHostName);   
              
    for(i = 0; pHost != NULL && pHost->h_addr_list[i]!=NULL; i++)   
    {   
    char   strHostIp[25];  
    memset(strHostIp, 0, 25); 
    int   j;   
              
    for(j = 0; j < pHost->h_length; j++)   
    {   
    char  addr[10];   
               if(j > 0)   
    strcat(strHostIp, ".");   
               sprintf(addr, "%u",(unsigned   int)((unsigned char*)pHost->h_addr_list[i])[j]);   
    strcat(strHostIp, addr);   
    }
    strcpy(szIp, strHostIp);
    }   
    }
    return true;
    }
      

  2.   


    CString LocalIP;
    hostent *name;
    char Name[255];
    memset(Name,0,255);
    gethostname(Name,255);
    name=gethostbyname(Name);
    LocalIP=inet_ntoa(*(in_addr *)name->h_addr_list[0]);
    最好去 MSDN 查查相关函数及其用法,养成好习惯...
      

  3.   

    //可以打印多个网卡的IP
    void print_all_ip(void)
    {
      char szHostName[128];
      const char* pszAddr;
      struct hostent * pHost;
      int i,j; 
      if( gethostname(szHostName, 128) == 0 )
      {
        pHost = gethostbyname(szHostName); 
        for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 
        {/*对每一个IP地址进行处理*/
          pszAddr=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
          printf("%s\n",pszAddr);/*打印*/
        }
      }
    }
      

  4.   


    // 得到本机的IP地址
    char szHost[256];
    ::gethostname(szHost, 256);
    hostent* pHost = ::gethostbyname(szHost);