Programmatically check whether my machine has internet access or not

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

    There is nothing of that sort I think, but you can try this:

    The easiest way is to try to connect to a known outside IP address.

    If it fails in Windows, the connect function will return SOCKET_ERROR, and WSAGetLastError will usually return WSAEHOSTUNREACH (meaning the packet couldn't be sent to the host).

    In Linux, you'll get back a -1, and errno will be ENETUNREACH. Some useful links:

    1. Link for Windows Sockets

    2. Link for Linux/Unix sockets

提交回复
热议问题