Ping IP dengan Delphi II
Pada artikel yang saya bahas sebelumnya (ping IP dengan Delphi) mempunyai kelemahan jika koneksi yang terhubung menggunakan jaringan radio maupun speedy alias bisa berfungsi dengan baik jika terhubung dalam sebuah jaringan lokal. Akhirnya saya coba cari-cari lagi, ternyata delphi sudah menyediakan komponen tersendiri untuk melakukan tugas semacam ini dengan bantuan komponen IdIcmpClient keluarga dari Indy Client. Kita langsung saja bahas aplikasi sederhana saja ya..
Pertama sediakan beberapa komponen diantaranya:
1. IdIcmpClient1 properties name ICMP
2. Memo1 properties name lstReplies
3. Timer1
4. Button1
5. Button2
source code lengkapnya ada sebagai berikit:
/////.....................................................................................
var
Form1: TForm1;
ipserver:string;
type
EidSocketError=class(Exception);
EFopenError=class(Exception);
implementation
{$R *.dfm}
procedure TForm1.ICMPReplyinduk(ASender: TComponent;
const ReplyStatus: TReplyStatus);
var
sTime: string;
begin
if (ReplyStatus.MsRoundTripTime = 0) then
sTime := '<1'
else
sTime := '=';
ipserver:=Format('%s', [ReplyStatus.FromIpAddress]);
if ipserver='127.0.0.1' then
begin
lstReplies.Items.Add(Format('Koneksi 127.0.0.1 Lancar: bytes=%d time%s%d ms TTL=%d',
[ReplyStatus.BytesReceived,
sTime,
ReplyStatus.TimeToLive,
ReplyStatus.MsRoundTripTime]));
end
else
begin
lstReplies.Items.Add(Format('Koneksi 127.0.0.1 Lemah: bytes=%d time%s%d ms TTL=%d',
[ReplyStatus.BytesReceived,
sTime,
ReplyStatus.TimeToLive,
ReplyStatus.MsRoundTripTime]));
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
try
ICMP.OnReply := ICMPReplyinduk;
ICMP.ReceiveTimeout := 1000;
ICMP.Host :='127.0.0.1' ;
ICMP.Ping;
Application.ProcessMessages;
except
begin
lstReplies.Items.Add('Tidak bisa terhubung ke jaringan');
end;
end;
end;
procedure TForm1.btncekClick(Sender: TObject);
begin
if btncek.Caption ='Cek Koneksi' then
begin
btncek.Caption:='Stop';
timer1.Enabled:=true;
end
else
if btncek.Caption ='Stop' then
begin
btncek.Caption:='Cek Koneksi';
timer1.Enabled:=false;
lstReplies.Clear;
end;
end;
procedure TForm1.btnexitClick(Sender: TObject);
begin
application.Terminate ;
end;
Form1: TForm1;
ipserver:string;
type
EidSocketError=class(Exception);
EFopenError=class(Exception);
implementation
{$R *.dfm}
procedure TForm1.ICMPReplyinduk(ASender: TComponent;
const ReplyStatus: TReplyStatus);
var
sTime: string;
begin
if (ReplyStatus.MsRoundTripTime = 0) then
sTime := '<1'
else
sTime := '=';
ipserver:=Format('%s', [ReplyStatus.FromIpAddress]);
if ipserver='127.0.0.1' then
begin
lstReplies.Items.Add(Format('Koneksi 127.0.0.1 Lancar: bytes=%d time%s%d ms TTL=%d',
[ReplyStatus.BytesReceived,
sTime,
ReplyStatus.TimeToLive,
ReplyStatus.MsRoundTripTime]));
end
else
begin
lstReplies.Items.Add(Format('Koneksi 127.0.0.1 Lemah: bytes=%d time%s%d ms TTL=%d',
[ReplyStatus.BytesReceived,
sTime,
ReplyStatus.TimeToLive,
ReplyStatus.MsRoundTripTime]));
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
try
ICMP.OnReply := ICMPReplyinduk;
ICMP.ReceiveTimeout := 1000;
ICMP.Host :='127.0.0.1' ;
ICMP.Ping;
Application.ProcessMessages;
except
begin
lstReplies.Items.Add('Tidak bisa terhubung ke jaringan');
end;
end;
end;
procedure TForm1.btncekClick(Sender: TObject);
begin
if btncek.Caption ='Cek Koneksi' then
begin
btncek.Caption:='Stop';
timer1.Enabled:=true;
end
else
if btncek.Caption ='Stop' then
begin
btncek.Caption:='Cek Koneksi';
timer1.Enabled:=false;
lstReplies.Clear;
end;
end;
procedure TForm1.btnexitClick(Sender: TObject);
begin
application.Terminate ;
end;
/////.....................................................................................
Monggo dicoba semoga bermanfaat...
5 komentar:
anjing kalau ngasih selalu setengah setengah setan .... buat newbie bingung anjing
kenapa/
ada error ni gan
[Error] Unit1.pas(38): Undeclared identifier: 'ICMPReplyinduk'
saya udah tambah procedure
procedure ICMPReplyinduk();
tetep ga bisa
Thanks gan... works :)
Trims
Posting Komentar