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

相关文章:

  • 2021-04-13
  • 2021-09-29
  • 2021-11-02
  • 2021-12-20
  • 2021-11-16
  • 2021-08-25
猜你喜欢
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-05-12
  • 2021-09-05
相关资源
相似解决方案