Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 May : Re: Delphi 2005 Personal and Indy 10.0.75: Indy don't work!!!
| Subject: | Re: Delphi 2005 Personal and Indy 10.0.75: Indy don't work!!! |
| Posted by: | "Jamie Dale" (j.da..@turboz.net) |
| Date: | Tue, 6 Jun 2006 01:52:45 |
> If your code is detecting changes every 5 seconds, then at least one of
> those values is
> not accurate. That suggests a bug in the way you are obtaining the IPs in
> the first place before comparing them. Please show your actual code.
Sure - It's below my other answers.
> Are you taking multiple network adapters into account?
Yes, the IP is checked via an external website using IdHTTP component. I
don't bother checking network adapters - way too much trouble...
> Then please show the actual code.
It's long, somewhat sloppy (I KNOW SO PLEASE DON'T SLATE ME FOR IT), but
normally effective until it's run on machine number 2. Works perfectly on my
normal PC.
Here it is:
procedure TMain.Timer1Timer(Sender: TObject);
//This procedure checks current localhost IP using a remote website, checks
the subdomains and updates if needed.
var
Response, IP: String;
I: Integer;
Matches: Boolean;
begin
//No matches yet so set to false
Matches := False;
If we got sites/subdomains in array
If Length(Site) >= 1 then
begin
//go through them all
For I := 0 to Length(Site) -1 do
begin
//If this site is enabled for dynamic updates
If Site[I].Enabled then
begin
//we have a match!!!! - Subdomain enabled - we must
check/update if IP has changed - continue & check
Matches := True;
//Get outta here
Break;
end;
end;
//If none of the subdomains were enabled
If not Matches then
begin
//Exit entire procedure
Exit;
end;
end;
//Disable timer so that HTTP can have it's time to call update URLs and get
response
Timer1.Enabled := False;
//If nothing in array, but we have username/password
If (Length(Site) = 0) and (User <> '') and (Password <> '') then
begin
//load account details
Button1Click(self);
end;
Try
//Add contact details to header just incase dyndns get funny about
this..
IdHTTP1.Response.ExtraHeaders.Add('Turboz IP Checker; Contact:
dale_jamie@yahoo.com');
//Get current IP from dyndns.org
Response := IdHTTP1.Get('http://checkip.dyndns.org');
Except
//Incase we don't get response (EG ISP disconnect) re-enable timer
to try again later
Timer1.Enabled := True;
//Exit
Exit;
End;
//Response is in HTML format:
//<html><head><title>Current IP Check</title></head><body>Current IP
Address: 81.78.183.186</body></html>
//Use custom function to get content after : symbol
IP := Parse(':', Response, 2);
//IP = 81.78.183.186</body></html>
//Delete everything after and including < symbol
Delete(IP, Pos('<', IP), Length(IP));
//Remove spaces
IP := Trim(IP);
//Go through all sites in array
For I := 0 to Length(Site) -1 do
begin
IF current IP <> Site[?].IP and the site is enabled
If (IP <> Site[I].IP) and (Site[I].Enabled) then
begin
//Add date/time to memo
Memo1.Lines.Add(DateToStr(Date) + ' ' + TimeToStr(Time));
//Log IP change to memo
Memo1.Lines.Add('IP has changed: Trying to update hosts..');
<== Every 5 seconds?
Try
//Call the update URL for that subdomain
Response := IdHTTP1.Get(Site[I].URL);
//If we find the word 'Updated' in the response
string
If Pos('Updated', Response) >= 1 then
begin
//Date/Time
Memo1.Lines.Add(DateToStr(Date) + ' ' +
TimeToStr(Time));
//Log the subdomain that was updated
Memo1.Lines.Add('Updated: ' +
Site[I].Domain);
//If Tabsheet is visible scroll it,
otherwise don't bother
If (TabSheet2.Visible) AND (Visible) then
ScrollLog;
//Get updated IP from response {updated to
***.***.***.*** in x seconds}
Response := Copy(Response, Pos('to',
Response) + 2, Length(Response));
Response := Copy(Response, 1, Pos('in',
Response) - 2);
//Set the subdomain with the IP the remote
DNS server now holds
Site[I].IP := Trim(Response);
end;
Except
//If any of that failed, try again later and exit
Timer1.Enabled := True;
Exit;
End;
end
else
begin
//Date/Time
Memo1.Lines.Add(DateToStr(Date) + ' ' + TimeToStr(Time));
//Log no update needed
Memo1.Lines.Add(Site[I].Domain + ' No update needed.');
//If tabsheet2 is showing scrollit, otherwise don't
If (TabSheet2.Visible) AND (Visible) then
ScrollLog;
end;
end;
//Enable timer for the next IP check
Timer1.Enabled := True;
end;
procedure TMain.Button1Click(Sender: TObject);
//Downloads XML file, grabs subdomains, current IP, Update URL of all
subdomains in my account
var
StartItemNode : IXMLNode;
ANode : IXMLNode;
URL: String;
begin
CheckListBox1.Clear;
URL := 'http://freedns.afraid.org/api/?action=getdyndns&username='
+ User + '&password=' + Password + '&style=xml';
If (User <> '') AND (Password <> '') AND (DownloadFile(URL, 'domains.xml'))
then
begin
Memo1.Lines.Add(DateToStr(Date) + ' ' + TimeToStr(Time));
Memo1.Lines.Add('Downloaded host data');
If (TabSheet2.Visible) AND (Visible) then
ScrollLog;
XML1.FileName := 'domains.xml';
XML1.Active := True;
StartItemNode := XML1.DocumentElement.ChildNodes.FindNode('item');
Anode := StartItemNode;
If Anode <> nil then
begin
Memo1.Lines.Add(DateToStr(Date) + ' ' + TimeToStr(Time));
Memo1.Lines.Add('Parsing Hosts xml.. Please wait');
If (TabSheet2.Visible) AND (Visible) then
ScrollLog;
SetLength(Site, 0);
repeat
SetLength(Site, Length(Site) + 1);
Site[Length(Site) -1].Domain :=
Anode.ChildNodes['host'].Text;
Site[Length(Site) -1].IP :=
Anode.ChildNodes['address'].Text;
Site[Length(Site) -1].URL := Anode.ChildNodes['url'].Text;
Anode := Anode.NextSibling;
Until ANode = nil;
end;
XML1.Active := False;
CheckDomainMatch;
UpdateView;
end;
end;
procedure CheckDomainMatch;
//Checks hosts in array (from XML) to see if they are recorded in
'freedns.ini'
//If found in 'freedns.ini' then we update this host when IP changes.
var
I, J: Integer;
Found: Boolean;
begin
Found := False;
Main.Memo1.Lines.Add(DateToStr(Date) + ' ' + TimeToStr(Time));
//THIS NEXT LINE is the last comment recorded before the program seems to
stop doing anything.
Main.Memo1.Lines.Add('Checking hosts to update..');
For I := 0 to Length(Main.Site) -1 do
begin
For J := 0 to Length(Main.Host) -1 do
begin
If Main.Site[I].Domain = Main.Host[J].Domain then
begin
Main.Site[I].Enabled := True;
Found := True;
Main.Memo1.Lines.Add(DateToStr(Date) + ' ' +
TimeToStr(Time));
Main.Memo1.Lines.Add('Host found: ' +
Main.Site[I].Domain);
If Main.TabSheet2.Visible then
ScrollLog;
end;
end;
end;
If not Found then
begin
Main.Memo1.Lines.Add(DateToStr(Date) + ' ' + TimeToStr(Time));
Main.Memo1.Lines.Add('Nothing to update!');
end;
end;
procedure UpdateView;
//Simply displays all hosts and whether or not we are updating them.
var
I: Integer;
begin
For I := 0 to Length(Main.Site) -1 do
begin
Main.CheckListBox1.Items.Add(Main.Site[I].Domain);
Main.CheckListBox1.Checked[Main.CheckListBox1.Count -1] :=
Main.Site[I].Enabled;
end;
end;