获得本计算机的名字和IP地址

WORD wVersionRequested;
        WSADATA wsaData;
        char name[255];
        CString ip;
        PHOSTENT hostinfo;
        wVersionRequested = MAKEWORD( 2, 0 );

        if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
        {

              if( gethostname ( name, sizeof(name)) == 0)
              {
                    if((hostinfo = gethostbyname(name)) != NULL)
                    {
                          ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                    }
              }
           
              WSACleanup( );
        }
        AfxMessageBox(name);//name里是本机名
        AfxMessageBox(ip);  //ip中是本机IP

posted on 2006-06-24 07:19  路吾  阅读(461)  评论(1编辑  收藏  举报

导航