TIdIPWatch控件可以很简单的获取本机IP
 IdIPWatch1.LocalIP 就是本机IP了
但是获取的不正确本机IP如下Ethernet adapter 本地连接:        Connection-specific DNS Suffix  . :
        Autoconfiguration IP Address. . . : 169.254.112.198
        Subnet Mask . . . . . . . . . . . : 255.255.0.0
        Default Gateway . . . . . . . . . :PPP adapter 宽带连接:        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 123.123.123.123
        Subnet Mask . . . . . . . . . . . : 255.255.255.255
        Default Gateway . . . . . . . . . : 123.123.123.123
使用 IdIPWatch1.LocalIP 总是获得169.254.112.198 , 但是我想获得的是 宽带连接IP 也就是 外网IP.请教这该怎么做?

解决方案 »

  1.   

    有个取巧的方法就是去那些有显示外网IP的网站截取HTML来分析,不过最好找个稳定点的。
      

  2.   


    Function   LocalIP:string;   
      type   
      TapinAddr=array[0..10]of   Pinaddr;   
      papinaddr=^tapinaddr;   
      var   
      phe:phostent;   
      pptr:papinaddr;   
      buffer:array[0..63]   of   char;   
      i:integer;   
      ginitdata:twsadata;   
      begin   
      result:='';   
      wsastartup($101,ginitdata);   
      try   
      gethostname(buffer,sizeof(buffer));   
        phe:=gethostbyname(buffer);   
        if   phe<>nil   then   
        begin   
        pptr:=papinaddr(phe^.h_addr_list);   
        I:=0;   
        while   pptr^[i]<>nil   do   
        begin   
        result:=strpas(inet_ntoa(pptr^[i]^));   
        inc(i);   
        end;   
        end;   
        finally   
        wsacleanup;   
        end;   
        end;   
      procedure   TForm1.Button1Click(Sender:   TObject);   
      begin   
        edit1.Text:=localIP;   //取出IP   
      end;