Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check if an IP Address exists or not?

Networking

How do I check if an IP Address exists or not?

by  djjd47130  Posted    (Edited  )
This function uses the Indy Clients, and checks if a given IP Address is valid or not by Pinging that IP. If it receives anything back, it returns true, or else if it does not get a reply, it returns false.

Code:
[navy][i]// for automatic syntax highlighting see faq102-6487 
[/i][/navy]
[b]unit[/b] IPVerify;

[b]interface[/b]

[b]uses[/b]
  IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient, 
  Windows, Messages, Classes, SysUtils;

[b]function[/b] IPExists(IPAddr: String): Bool;

[b]implementation[/b]

[b]function[/b] IPExists(IPAddr: String): Bool;
[b]var[/b]
  I: TIdIcmpClient;
  Rec: Integer;
[b]begin[/b]
  Result:= False;
  I:= TIdIcmpClient.Create([b]nil[/b]);
  [b]try[/b]
    I.Host:= IPAddr;
    I.Ping();
    Sleep([navy]2000[/navy]);
    Rec:= I.ReplyStatus.BytesReceived;
    [b]if[/b] Rec > [navy]0[/navy] [b]then[/b] Result:= True [b]else[/b] Result:= False;
  [b]finally[/b]
    I.Free;
  [b]end[/b];
[b]end[/b];

[b]end[/b].
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top