我用XML,idhttp,从网址取IP,很简单的功能,
function GetIP: string;
Var
xml : OleVariant;
r,ipstr:string;
p1,p2,i,ii,count : Integer;
begin
xml := CreateOleObject('Microsoft.XMLHTTP');
xml.Open('GET','http://www.baidu.com/s?wd=ip', False);
xml.Send;
r := xml.responseText;
p1:=Pos('本机IP: <strong>',r);
p2:=Pos('</strong> ',r);
ipstr := Copy(r, p1+7, p2-p1+2);
//showmessage(ipstr);
ipstr := StringReplace (ipstr, ' ', ' ', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, ' ', '', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, '<strong>', '', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, '</strong>', '', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, '</p>', '', [rfReplaceAll, rfIgnoreCase]);
end;功能就是,在界面上点击按钮就取得IP,但是,问题来了,我如果断开路由器,重新连接换了IP之后,点击这个按钮,他返回的依然是换之前的ip,如果我退出程序,重新再进来,再点击按钮,就返回了换之后的ip了,这怎么解决?因为我在不关闭程序的前提了,换一次ip,就需要知道最新的ip是多少,这样就无法实现了。
路由器
function GetIP: string;
Var
xml : OleVariant;
r,ipstr:string;
p1,p2,i,ii,count : Integer;
begin
xml := CreateOleObject('Microsoft.XMLHTTP');
xml.Open('GET','http://www.baidu.com/s?wd=ip', False);
xml.Send;
r := xml.responseText;
p1:=Pos('本机IP: <strong>',r);
p2:=Pos('</strong> ',r);
ipstr := Copy(r, p1+7, p2-p1+2);
//showmessage(ipstr);
ipstr := StringReplace (ipstr, ' ', ' ', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, ' ', '', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, '<strong>', '', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, '</strong>', '', [rfReplaceAll, rfIgnoreCase]);
ipstr := StringReplace (ipstr, '</p>', '', [rfReplaceAll, rfIgnoreCase]);
end;功能就是,在界面上点击按钮就取得IP,但是,问题来了,我如果断开路由器,重新连接换了IP之后,点击这个按钮,他返回的依然是换之前的ip,如果我退出程序,重新再进来,再点击按钮,就返回了换之后的ip了,这怎么解决?因为我在不关闭程序的前提了,换一次ip,就需要知道最新的ip是多少,这样就无法实现了。
路由器
function GetIP(ASocket: TSocket): string;
var
ALen: Integer;
Addr: Sockaddr_In;
begin
Result := '';
if ASocket > 0 then
begin
ALen := sizeof(Sockaddr_In);
GetPeerName(ASocket,Addr,ALen);
Result := inet_ntoa(Addr.sin_addr);
end;
end;