Programmatically check whether my machine has internet access or not

前端 未结 6 1588
面向向阳花
面向向阳花 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:16

    The following code will work, if you're on windows:

    #include 
    #include 
    
    int main(){
    
      if (system("ping www.google.com")){
              std::cout<<"\nNot connnected to the internet\n\n";
      }
      else{
              std::cout<<"\nConnected to the internet\n\n";
    
      }
      system("pause");
      return 0;
    }
    

提交回复
热议问题