请问:如何在VC里得到一个域名的IP地址.如,怎样得到http://www.sohu.com的IP地址

解决方案 »

  1.   

    谢谢你,"raphyer(把脚印留在身后) "
    能说详细点吗?举个例子?
    谢谢
      

  2.   

    SOCKADDR_IN addr_in;
    addr_in.sin_family = AF_INET;
    addr_in.sin_port = htons(iPort);
    USES_CONVERSION;
    char* pszServer = W2A(bstrServer);
    ULONG ulAddr = inet_addr(pszServer);
    if( INADDR_NONE != ulAddr ) // ip
    {
    addr_in.sin_addr.s_addr = ulAddr;
    }
    else // 域名
    {
    hostent* pHost = gethostbyname( pszServer );
    memcpy((char*)&addr_in.sin_addr,pHost->h_addr,pHost->h_length);
    }
      

  3.   

    请问 (xiaohyy(醉大饿极)) :要包括哪些头文件
      

  4.   

    #include<winsock2.h>     WORD wVersionRequested;
          WSADATA wsaData;
          char name[255];
          CString ip;
          PHOSTENT hostinfo;
          wVersionRequested = MAKEWORD( 2, 0 );
    sprintf(name,"%s","http://www.sohu.com");
          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( );
          } 
      

  5.   

    if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR) { fprintf(stderr,"WSAStartup failed with error %d\n",WSAGetLastError()); WSACleanup(); return -1; }  if (port == 0){ Usage(argv[0]); }  // // Attempt to detect if we should call gethostbyname() or // gethostbyaddr()  if (isalpha(server_name[0])) {   /* server address is a name */ hp = gethostbyname(server_name); } else  { /* Convert nnn.nnn address to a usable one */ addr = inet_addr(server_name); hp = gethostbyaddr((char *)&addr,4,AF_INET); } if (hp == NULL ) { fprintf(stderr,"Client: Cannot resolve address [%s]: Error %d\n", server_name,WSAGetLastError()); WSACleanup(); exit(1); }
      

  6.   

    {
    DWORD dwIP = INADDR_NONE;
    PHOSTENT pHost = gethostbyname( (char *)pParam );
    if(pHost == NULL)
    return INADDR_NONE;
    dwIP = inet_addr( inet_ntoa(*(IN_ADDR *)*pHost->h_addr_list) );
    return dwIP;
    }