//判斷一個機器的MSSQL是否啟動,通過SQL DMO是可以的,但對於沒有裝MSSQL的客戶端來說就沒辦法,此處用的是連接MSSQL的1433端口,如果端口號不同,可以通過傳遞端口.
unit Judge_U;
interface
uses
SysUtils, Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdIcmpClient,
IdTCPClient, IdRawBase, IdRawClient;
function JudgePort(AServerName: PChar; APort: Integer): Boolean;
function JudgePing(AServerName: PChar): Boolean;
implementation
function JudgePing(AServerName: PChar): Boolean;//這個是用來PIN計算機的.
var
ICMP: TIdIcmpClient;
begin
ICMP := TIdIcmpClient.Create(nil);
ICMP.ReceiveTimeout := 1000;
ICMP.Host := AServerName;
try
ICMP.Ping;
Result := True;
except
Result := False;
end;
ICMP.Free;
end;
function JudgePort(AServerName: PChar; APort: Integer): Boolean;
var
IdTCPClient1: TIdTCPClient;
begin
IdTCPClient1 := TIdTCPClient.Create(nil);
IdTCPClient1.Host := AServerName;
IdTCPClient1.Port := APort;
try
IdTCPClient1.Connect;
Result := True;
IdTCPClient1.Disconnect;
except
Result := False;
end;
IdTCPClient1.Free;
end;
end.
//有以下已知的BUG.
//1 如果一台計算機上安裝了多個實例.
//2 如果不用TCP/IP協議,而用其它的連接方式,比如典型的命名管道,就無法判斷.