TIdTCPClient应用实例

function PingAlive(const AHost: string; const ATimeout: Cardinal = 1000): boolean;
var
IdIcmpClient: TIdIcmpClient;
begin
try
IdIcmpClient := TIdIcmpClient.Create(nil);
IdIcmpClient.ReceiveTimeout := ATimeout; //設定回應的等候時間
IdIcmpClient .Host := AHost;
Result := true;
try
IdIcmpClient.Ping;
except
Result := false;
end;
finally
FreeAndNil(IdIcmpClient);
end;
end;

function CheckServerAlive(const AHost: string; const APort: integer): boolean;
var
IdTCPClient: TIdTCPClient;
begin
Result := false;

if not PingAlive(AHost) then
Exit;

try
IdTCPClient := TIdTCPClient.Create(nil);
IdTCPClient.Host := AHost;
IdTCPClient.Port := APort;

try
IdTCPClient.Connect;
Result := true;
except
Result := false;
end;
IdTCPClient.Disconnect;
finally
FreeAndNil(IdTCPClient);
end;
end;

procedure TFormLogin.Button1Click(Sender: TObject);
begin
if CheckServerAlive(Edit1.Text,3306) then ShowMessage('OK');
end;   

转自:http://hi.baidu.com/050502/blog/item/2f572d1f811156cea786699e.html

posted @ 2012-01-18 16:05  stma  阅读(2376)  评论(0)    收藏  举报