Programmatically check whether my machine has internet access or not

前端 未结 6 1580
面向向阳花
面向向阳花 2020-12-17 17:23

How can I programmatically check whether my machine has internet access or not using C/C++, is it just a matter of pinging an IP? How does NIC do it ? I mean something like

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 18:03

    If you work on Windows, just try this

    #include 
    #include  
    #include 
    using namespace std;
    
    int main(){
    
    if(InternetCheckConnection(L"http://www.google.com",FLAG_ICC_FORCE_CONNECTION,0))
    {
            cout << "connected to internet";
    }
    
    return 0;
    }
    

提交回复
热议问题