ورود

View Full Version : سوال: اطلاع از اتصال به اینترنت



سیروس مقصودی
یک شنبه 18 تیر 1402, 11:15 صبح
با سلام

من چگونه میتوانم بفهمم که دستگاه به اینترنت وصل هستش یا نه ؟

با تشکر

یوسف زالی
یک شنبه 18 تیر 1402, 21:19 عصر
سلام. یک سرور سریع رو پینگ بگیرید

دلفــي
سه شنبه 20 تیر 1402, 08:30 صبح
با سلام

من چگونه میتوانم بفهمم که دستگاه به اینترنت وصل هستش یا نه ؟

با تشکر

برای بررسی اتصال به اینترنت در دلفی می‌توانید از تابع TIdHTTP استفاده کنید. این تابع به وسیله بررسی اتصال به یک سایت مشخص (مثل گوگل)، می‌تواند مشخص کند که آیا دستگاه به اینترنت متصل است یا خیر. به عنوان مثال می‌توانید از کد زیر استفاده کنید:


uses
IdHTTP;

function IsConnectedToInternet: Boolean;
var
Http: TIdHTTP;
begin
Http := TIdHTTP.Create(nil);
try
Http.Head('http://www.google.com/');
Result := True;
except
on E: Exception do
Result := False;
end;
Http.Free;
end;


اگر تابع IsConnectedToInternet را فراخوانی کنید، در صورتی که دستگاه به اینترنت متصل باشد، true بازگردانده می‌شود و در غیر این صورت تابع false بازگردانده می‌شود.

ابوالفضل عباسی
چهارشنبه 24 آبان 1402, 11:00 صبح
اینم دستور مشابه با کامند ping در cmd هستش:

function Ping(const Address:string;Retries,BufferSize:Word;mm:TMemo):B oolean;var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
i : Integer;


PacketsReceived : Integer;
Minimum : Integer;
Maximum : Integer;
Average : Integer;


function GetStatusCodeStr(statusCode:integer) : string;
begin
case statusCode of
0 : Result:='Success';
11001 : Result:='Buffer Too Small';
11002 : Result:='Destination Net Unreachable';
11003 : Result:='Destination Host Unreachable';
11004 : Result:='Destination Protocol Unreachable';
11005 : Result:='Destination Port Unreachable';
11006 : Result:='No Resources';
11007 : Result:='Bad Option';
11008 : Result:='Hardware Error';
11009 : Result:='Packet Too Big';
11010 : Result:='Request Timed Out';
11011 : Result:='Bad Request';
11012 : Result:='Bad Route';
11013 : Result:='TimeToLive Expired Transit';
11014 : Result:='TimeToLive Expired Reassembly';
11015 : Result:='Parameter Problem';
11016 : Result:='Source Quench';
11017 : Result:='Option Too Big';
11018 : Result:='Bad Destination';
11032 : Result:='Negotiating IPSEC';
11050 : Result:='General Failure'
else
result:='Unknow';
end;
end;
begin;
PacketsReceived:=0;
Minimum :=0;
Maximum :=0;
Average :=0;
// Writeln(Format('Pinging %s with %d bytes of data:',[Address,BufferSize]));
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
//FWMIService := FSWbemLocator.ConnectServer('192.168.52.130', 'root\CIMV2', 'user', 'password');
for i := 0 to Retries-1 do
begin
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_PingStatus where Address=%s AND BufferSize=%d',[QuotedStr(Address),BufferSize]),'WQL',0);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, FWbemObject, iValue) = 0 then
begin
if FWbemObject.StatusCode=0 then
begin
Result := true;
if FWbemObject.ResponseTime>0 then
mm.Lines.Add(Format('Reply from %s: bytes=%s time=%sms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize, FWbemObject.ResponseTime,FWbemObject.TimeToLive]))
else
mm.Lines.Add(Format('Reply from %s: bytes=%s time=<1ms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize, FWbemObject.TimeToLive]));


Inc(PacketsReceived);


if FWbemObject.ResponseTime>Maximum then
Maximum:=FWbemObject.ResponseTime;


if Minimum=0 then
Minimum:=Maximum;


if FWbemObject.ResponseTime<Minimum then
Minimum:=FWbemObject.ResponseTime;


Average:=Average+FWbemObject.ResponseTime;
end
else
begin
Result := False;
if not VarIsNull(FWbemObject.StatusCode) then
mm.Lines.Add(Format('Reply from %s: %s',[FWbemObject.ProtocolAddress,GetStatusCodeStr(FWbem Object.StatusCode)]))
else
mm.Lines.Add(Format('Reply from %s: %s',[Address,'Error processing request']));
end;
end;
FWbemObject:=Unassigned;
FWbemObjectSet:=Unassigned;
//Sleep(500);
end;


mm.Lines.Add('');
mm.Lines.Add(Format('Ping statistics for %s:',[Address]));
mm.Lines.Add(Format(' Packets: Sent = %d, Received = %d, Lost = %d (%d%% loss),',[Retries,PacketsReceived,Retries-PacketsReceived,Round((Retries-PacketsReceived)*100/Retries)]));
if PacketsReceived>0 then
begin
mm.Lines.Add('Approximate round trip times in milli-seconds:');
mm.Lines.Add(Format(' Minimum = %dms, Maximum = %dms, Average = %dms',[Minimum,Maximum,Round(Average/PacketsReceived)]));
end;
end;