我现在通过局域网上网
每次上网,我的机器应该有个动态的IP
我用如下函数
void CBetSDlg::myIP(CString& IP)
{
WORD wVersion;
WSADATA wsaData;

char name[255];
PHOSTENT hostinfo;

wVersion=MAKEWORD(2,0);
if(WSAStartup(wVersion,&wsaData)==0)
{
if(gethostname(name,sizeof(name))==0)
{
if((hostinfo=gethostbyname(name))!=NULL)
{
IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
}
}
WSACleanup();
}
}
为什么总是得到局域网中的地质?如何获取动态分配的IP呢?

解决方案 »

  1.   

    PHOSTENT是个链表结构
    while(hostinfo->next)
    {
    hostinfo = hostinfo->next;
    IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);}
      

  2.   

    http://www.eaoo.com/design/list.asp?classid=2&Nclassid=13
      

  3.   

    hostinfo 哪有next变量了??
      

  4.   

    //这是MSDN的例子,我也没试过,你试试吧
       char     Hostname[100];
       HOSTENT *pHostEnt;
       int      nAdapter = 0;   gethostname( Hostname, sizeof( Hostname ));
       pHostEnt = gethostbyname( Hostname );   while ( pHostEnt->h_addr_list[nAdapter] )
       {
          // pHostEnt->h_addr_list[nAdapter] -the current address in host order
          nAdapter++;
       }
      

  5.   

    如果ADSL,ISDN,Modem只连接一台机器,你可以取到动态IP[包括移动无限上网]
    如果共享或者代理上网,你不可能取到动态IP
      

  6.   

    只有直接连到ISP的主机有动态IP
      

  7.   

    void CBetSDlg::myIP(CString& IP)
    {
    WORD wVersion;
    WSADATA wsaData;

    char name[255];
    PHOSTENT hostinfo;

    wVersion=MAKEWORD(2,0);
    if(WSAStartup(wVersion,&wsaData)==0)
    {
    if(gethostname(name,sizeof(name))==0)
    {
    //IP=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
        
    int index=0; //罗列本机的所有IP,上网后动态IP是最后一个
    while(hostinfo->h_addr_list[index]) //added   hostinfo是个连表结构
    {
    CString s=inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[index]);
    IP.Format("%s/n%s",IP,s);
    //AfxMessageBox(IP);
    index++;
    }
    }
    WSACleanup();
    }
    }