Internet connection Detection

Source: Internet
Author: User

 

Ideas:

 

1. First, check the current Nic status. If the NIC status is down, there is no need to perform the following test and it is determined that bool checknetstatus () is not connected to the Internet ()

2. If the NIC status is up, check whether a URL int checknet (char * URL, char * keystr) can be accessed)

 

 

 

Check the status of the current Nic. If the NIC status is down, you do not need to perform the following test to determine that the NIC is not connected to the Internet.

 

Bool checknetstatus ()

Bool checknetstatus () <br/>{< br/> bool netstatus = true; // The default status is networked <br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test result, obtain enough space <br/> If (getiftable (iftable, & dwsize, 0) = error_insufficient_buffer) {<br/> // release the space applied before the iftable, not processed <br/> iftable = (Mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0) = no_error) <br/> {<br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <br/> If (strstr (PTR, "USB") | strstr (PTR, "loopback ")) <br/> continue; <br/> dwretval = getifentry (& ifrow); <br/> If (if Row. dwadminstatus = mib_if_admin_status_down) <br/> netstatus = false; <br/> else <br/> netstatus = true; <br/>}< br/> else <br/> {<br/> const char * notice11 = "getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable); <br/> return netstatus; <br/>}< br/> 

Check whether a website can be accessed

# Include <PM. h> <br/> # include <iphlpapi. h> <br/> # pragma comment (Lib, "iphlpapi. lib ") <br/> # define maxsize 4096 <br/> # pragma comment (Lib," wininet. lib ") <br/> int checknet (char * URL, char * keystr) <br/>{< br/> int flag = 0; <br/> hinternet hsession = internetopen (_ T ("urltest"), internet_open_type_preconfig, null, null, 0); <br/> If (hsession! = NULL) <br/>{< br/> wchar_t * Wurl = short2wide (URL); <br/> hinternet hhttp = internetopenurl (hsession, Wurl, null, 0, internet_flag_dont_cache, 0); <br/> If (hhttp! = NULL) <br/>{< br/> char temp [maxsize]; <br/> ulong number = 1; <br/> while (number> 0) <br/>{< br/> memset (temp, 0, maxsize); <br/> internetreadfile (hhttp, temp, maxsize-1, & number ); <br/> If (strstr (temp, keystr )! = 0) <br/>{< br/> flag = 1; <br/> continue; <br/>}< br/> internetclosehandle (hhttp); <br/> hhttp = NULL; <br/>}< br/> internetclosehandle (hsession); <br/> hsession = NULL; <br/>}< br/> return flag; <br/>} 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.