得到本机和其它机器的IP地址和网卡MAC地址,是不是必须用PLATFORM?还有别的方法吗?

解决方案 »

  1.   

    采用下面的代码可以获得本机IP地址: 
        char szHostName[128]; 
         
        if( gethostname(szHostName, 128) == 0 ) 
        { 
         // Get host adresses 
         struct hostent * pHost; 
         int i; 
         
         pHost = gethostbyname(szHostName); 
         
         for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 
         { 
         CString str; 
         int j; 
         
         for( j = 0; j < pHost->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) 
         } 
        } 
      

  2.   

    //得到mac
    void main(argc, argv)
    int argc;
    char **argv;
    { struct hostent *hp;
    struct in_addr *ptr; /* get the server name */
    if ((hp = gethostbyname("skinner")) == NULL)
    {
    fprintf(stderr, "%s: server unknown.\n", *argv);
    exit (1);
    } /* get the address */
    if(hp->h_addrtype = AF_INET)
    {
    while((ptr=(struct in_addr *) *hp->h_addr_list++) != NULL)
    fprintf(stderr, "address: %s \n", inet_ntoa(*ptr));
    } exit(0);
    }