線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3220
推到 Plurk!
推到 Facebook!

如何快速偵測目前網路是否可以上網(瀏覽網頁或FTP)

缺席
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-08-11 12:54:51 IP:220.134.xxx.xxx 訂閱

想請教各位前輩:

由於目前撰寫的程式需要在ADSL撥接環境下執行,不知道要怎麼如何快速偵測目前網路是否可以上網(瀏覽網頁或FTP)

最好能再五秒內完成檢測,減少使用者等待,不知道要怎麼達成,自己目前想到用PING的方式,PING 中華電信的DNS不過不知道要怎麼實作,不知道是否有更好的方式或套件可以達成。

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-08-11 14:01:44 IP:61.219.xxx.xxx 未訂閱
How To Detecting If You Have a Connection to the Internet

資料來源:
function loadTOCNode(){}
Article ID : 242558
Last Review : July 1, 2004
Revision : 1.1
This article was previously published under Q242558
SUMMARY
loadTOCNode(1, 'summary');
Many of you want to know if a computer has an active Internet connection before trying to connect to the Internet using the WinInet API or some other communication interface. The internet connection is important if you don't want your application to cause Windows to automatically dial the default Internet modem connection.

This article provides a mechanism for determining if an Internet site is accessible without the risk of you being prompted to dial into another Internet Service Provider.
MORE INFORMATION
loadTOCNode(1, 'moreinformation');
Usually the best way to determine if you have a connection to a particular computer is to attempt the connection. If the autodial feature of Windows is enabled then attempting the connection may cause the default Internet dialup connectoid to be opened, and you will be prompted with your credentials to connect.

To avoid having the default Internet connectoid dialed, the InternetGetConnectedState function can be used to determine if there is a default Internet dialup connectoid configured and whether it is currently active or not. If there is a default Internet dialup connectoid configured and it is not currently active then InternetGetConnectedState will return FALSE. If InternetGetConnectedState returns TRUE then you can attempt to connect to the Internet resource without fear of being prompted to connect to another Internet Service Provider.

The following code demonstrates how you would do this:
if (InternetGetConnectedState(...) == FALSE)
{
    // Don't attempt connection or it will bring up the dialog
    ...
}
else
{
    //Attempt connection
    if (InternetOpenURL(...) == NULL)
    {
        // Call failed
        err = GetLastError();
        if ((err == ERROR_INTERNET_NAME_NOT_RESOLVED) ||
            (err == ERROR_INTERNET_CANNOT_CONNECT) ||
            (err == ERROR_INTERNET_TIMEOUT))
        {
            // probably not connected...handle appropriately
            ...
        }
    }
    // We're connected!!!
    ....
}                                    

You cannot rely solely on the fact that InternetGetConnectedState returning TRUE means that you have a valid active Internet connection. It is impossible for InternetGetConnectedState to determine if the entire connection to the Internet is functioning without sending a request to a server. This is why you need to send a request to determine if you are really connected or not. You can be assured however that if InternetGetConnectedState returns TRUE, that attempting your connection will NOT cause you to be prompted to connect to the default Internet Service Provider.

Be aware that InternetGetConnectedState only reports the status of the default Internet connectoid on Internet Explorer 4.x. If a nondefault connectoid is connected, InternetGetConnectedState always returns FALSE (unless a LAN connection is used). With Internet Explorer 4.x configured to use a LAN connection, InternetGetCo nectedState always returns TRUE.

Internet Explorer 5 behaves differently. If you are currently dialed into a different dial-up in Internet Explorer 5, InternetGetConnectedState reports dial-up connection status as long as any connectoid is dialed or an active LAN connection exists.

There are some other ways to try to determine if you currently have a connection to a particular network resource. The IsDestinationReachable() function can be used to find out if there is a current connection to an address. However, he IsDestinationReachable() function is only concerned with whether the IP address is reachable from your computer. It does not work through HTTP proxies or firewalls that restrict ICMP ping packets.

It is also possible to use RasEnumConnections to enhance your code so that you can tell if there is an active dialup connection that might have Internet access even though it is not the default Internet dialup connectoid.
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2006-08-11 14:04:31 IP:61.219.xxx.xxx 未訂閱

unit Unit1;

interface

uses
Windows, Messages, SysUtils, wininet,Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Types : Integer;
begin
Types := INTERNET_CONNECTION_MODEM
INTERNET_CONNECTION_LAN
INTERNET_CONNECTION_PROXY;
//check 3 conditions together
if InternetGetConnectedState(@types, 0) then
Edit1.Text := 'You Have a Connection to the Internet'
else
Edit1.Text := 'You not a Connection to the Internet';
end;

end.

g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#4 引用回覆 回覆 發表時間:2006-08-20 07:30:55 IP:220.134.xxx.xxx 訂閱

謝謝您的回覆,你所說得方法可以偵測網路狀態!

但是若於程式執行中拔除網路線,我發現偵測結果會錯誤!但若在斷線狀態時中斷程式然後在進入程式偵測一次
我發現偵測網路連線情況會正確,而且若連線中執行程式偵測,然後拔除網路線,我發現SKYPE偵測網路狀態的
也會變成持續偵測的情況,不知道是否都用到wininet模組,不知道怎麼作才可以跟SKYPE做到立即性的隨時偵測網路狀態,而非進入程式檢查而已!謝謝您!

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2006-09-15 22:00:44 IP:219.81.xxx.xxx 未訂閱
uses wininet;

// Get offline state
// Alhaiseb Misurata Libya

function IsGlobalOffline: Boolean;
var
State, Size: DWORD;
begin
Result := False;
State := 0;
Size := SizeOf(DWORD);
if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @State, Size)
then
if
(State and INTERNET_STATE_DISCONNECTED_BY_USER) <> 0
then
Result := True;
end;


//Set offline state

procedure SetGlobalOffline(fGoOffline: Boolean);
var
ci: INTERNET_CONNECTED_INFO;
begin
if
fGoOffline
then
begin
ci.dwConnectedState := INTERNET_STATE_DISCONNECTED_BY_USER;
ci.dwFlags := ISO_FORCE_DISCONNECTED;
end
else
ci.dwConnectedState := INTERNET_STATE_CONNECTED;
InternetSetOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ci, SizeOf(ci));
end;

------
努力會更接近成功
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#6 引用回覆 回覆 發表時間:2006-09-18 18:56:35 IP:220.134.xxx.xxx 訂閱

前輩:

您po上來的程式碼,我找過相關討論,主要是用於IE瀏覽器的離線瀏覽狀態切換,跟我說說,要偵測目前區域網路是否

可以連上區域網路並不相同,不過感謝您的提供。 希望能繼續找到相關討論! 謝謝!

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
系統時間:2024-04-29 12:27:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!