📄 howto detecting if you have a connection to the internet.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0054)http://go2.163.com/~favorites/vc/internetconnected.htm -->
<HTML xmlns="http://www.w3.org/TR/REC-html40" xmlns:o =
"urn:schemas-microsoft-com:office:office" xmlns:w =
"urn:schemas-microsoft-com:office:word"><HEAD><TITLE>HOWTO: Detecting If You Have a Connection to the Internet</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GB2312">
<META content=Word.Document name=ProgId>
<META content="MSHTML 5.50.4134.600" name=GENERATOR>
<META content="Microsoft Word 9" name=Originator><LINK
href="./internetconnected.files/filelist.xml" rel=File-List><!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>duan</o:Author>
<o:Template>Normal</o:Template>
<o:LastAuthor>duan</o:LastAuthor>
<o:Revision>2</o:Revision>
<o:TotalTime>8</o:TotalTime>
<o:Created>2001-04-05T02:12:00Z</o:Created>
<o:LastSaved>2001-04-05T02:12:00Z</o:LastSaved>
<o:Pages>3</o:Pages>
<o:Words>562</o:Words>
<o:Characters>3204</o:Characters>
<o:Company>上海永驰电子有限公司</o:Company>
<o:Lines>26</o:Lines>
<o:Paragraphs>6</o:Paragraphs>
<o:CharactersWithSpaces>3934</o:CharactersWithSpaces>
<o:Version>9.2812</o:Version>
</o:DocumentProperties>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:UseFELayout/>
</w:Compatibility>
</w:WordDocument>
</xml><![endif]-->
<STYLE>@font-face {
font-family: 宋体;
}
@font-face {
font-family: 黑体;
}
@font-face {
font-family: Verdana;
}
@font-face {
font-family: \@宋体;
}
@font-face {
font-family: \@黑体;
}
@page {mso-page-border-surround-header: no; mso-page-border-surround-footer: no; }
@page Section1 {size: 595.3pt 841.9pt; margin: 72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin: 42.55pt; mso-footer-margin: 49.6pt; mso-paper-source: 0; layout-grid: 15.6pt; }
P.MsoNormal {
TEXT-JUSTIFY: inter-ideograph; FONT-SIZE: 10.5pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"; TEXT-ALIGN: justify; mso-style-parent: ""; mso-pagination: none; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt
}
LI.MsoNormal {
TEXT-JUSTIFY: inter-ideograph; FONT-SIZE: 10.5pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"; TEXT-ALIGN: justify; mso-style-parent: ""; mso-pagination: none; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt
}
DIV.MsoNormal {
TEXT-JUSTIFY: inter-ideograph; FONT-SIZE: 10.5pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"; TEXT-ALIGN: justify; mso-style-parent: ""; mso-pagination: none; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 宋体; mso-font-kerning: 1.0pt
}
H2 {
FONT-WEIGHT: bold; FONT-SIZE: 18pt; MARGIN-LEFT: 0cm; MARGIN-RIGHT: 0cm; FONT-FAMILY: 宋体; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-outline-level: 2
}
P {
FONT-SIZE: 12pt; MARGIN-LEFT: 0cm; MARGIN-RIGHT: 0cm; FONT-FAMILY: 宋体; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto; mso-bidi-font-family: "Times New Roman"
}
PRE {
FONT-SIZE: 10pt; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: 黑体; mso-pagination: widow-orphan; mso-bidi-font-family: "Courier New"; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt; mso-hansi-font-family: "Courier New"
}
DIV.Section1 {
page: Section1
}
</STYLE>
</HEAD>
<BODY lang=ZH-CN style="TEXT-JUSTIFY-TRIM: punctuation; tab-interval: 21.0pt">
<DIV class=Section1 style="LAYOUT-GRID: 15.6pt none">
<H2><SPAN lang=EN-US style="FONT-FAMILY: Verdana">HOWTO: Detecting If You Have a
Connection to the Internet<o:p></o:p></SPAN></H2>
<H2><SPAN lang=EN-US style="FONT-FAMILY: Verdana">SUMMARY<o:p></o:p></SPAN></H2>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana">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.<BR><BR>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. <o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US
style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"><![if !supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<H2><SPAN lang=EN-US style="FONT-FAMILY: Verdana">MORE
INFORMATION<o:p></o:p></SPAN></H2>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana">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.<BR><BR>To
avoid having the default Internet connectoid dialed, the
<B>InternetGetConnectedState</B> 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 <B>InternetGetConnectedState</B> will return FALSE. If
<B>InternetGetConnectedState</B> returns TRUE then you can attempt to connect to
the Internet resource without fear of being prompted to connect to another
Internet Service Provider.<BR><BR>The following code demonstrates how you would
do this:<o:p></o:p></SPAN></P><PRE><SPAN lang=EN-US>if (InternetGetConnectedState(...) == FALSE)</SPAN></PRE><PRE><SPAN lang=EN-US>{</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>// Don't attempt connection or it will bring up the dialog</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>...</SPAN></PRE><PRE><SPAN lang=EN-US>}</SPAN></PRE><PRE><SPAN lang=EN-US>else</SPAN></PRE><PRE><SPAN lang=EN-US>{</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>//Attempt connection</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>if (InternetOpenURL(...) == NULL)</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>{</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>// Call failed</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>err = GetLastError();</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>if ((err == ERROR_INTERNET_NAME_NOT_RESOLVED) ||</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>(err == ERROR_INTERNET_CANNOT_CONNECT) ||</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>(err == ERROR_INTERNET_TIMEOUT))</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>{</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>// probably not connected...handle appropriately</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>...</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>}</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>}</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>// We're connected!!!</SPAN></PRE><PRE><SPAN lang=EN-US><SPAN style="mso-spacerun: yes"> </SPAN>....</SPAN></PRE><PRE><SPAN lang=EN-US>}</SPAN></PRE><PRE><SPAN lang=EN-US> </SPAN></PRE>
<P class=MsoNormal><SPAN lang=EN-US
style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"><BR>You cannot rely solely on the
fact that <B>InternetGetConnectedState</B> returning TRUE means that you have a
valid active Internet connection. It is impossible for
<B>InternetGetConnectedState</B> 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 <B>InternetGetConnectedState</B> returns TRUE, that
attempting your connection will NOT cause you to be prompted to connect to the
default Internet Service Provider.<BR><BR>Be aware that
<B>InternetGetConnectedState</B> only reports the status of the default Internet
connectoid on Internet Explorer 4.<I>x</I>. If a nondefault connectoid is
connected, InternetGetConnectedState always returns FALSE (unless a LAN
connection is used). With Internet Explorer 4.<I>x</I> configured to use a LAN
connection, InternetGetCo nectedState always returns TRUE.<BR><BR>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. <BR><BR>There are some other ways to try to determine if you
currently have a connection to a particular network resource. The
<B>IsDestinationReachable()</B> function can be used to find out if there is a
current connection to an address. However, he <B>IsDestinationReachable()</B>
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.<BR><BR>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.</SPAN></P></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -