What is the fastest way to check an internet connection using C or C++? -
i'm trying write program in c/c++ check internet connection on windows machine. appreciated. thank you.
i can show short way, based on microsoft example. in fact, way make sure can connect internet try connect server in internet. applications use google target-server or own server (if have one). modified example connect google on port 80. if program return 0, success, otherwise returns 1. should give startpoint own solution:
// example from: // https://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx #define win32_lean_and_mean #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> // need link ws2_32.lib, mswsock.lib, , advapi32.lib #pragma comment (lib, "ws2_32.lib") #pragma comment (lib, "mswsock.lib") #pragma comment (lib, "advapi32.lib") #define default_buflen 512 #define default_port "80" int __cdecl main(int argc, char **) { wsadata wsadata; socket connectsocket = invalid_socket; struct addrinfo *result = null, *ptr = null, hints; char *sendbuf = "this test"; char recvbuf[default_buflen]; int iresult; int recvbuflen = default_buflen; // initialize winsock iresult = wsastartup(makeword(2, 2), &wsadata); if (iresult != 0) { printf("wsastartup failed error: %d\n", iresult); return 1; } zeromemory(&hints, sizeof(hints)); hints.ai_family = af_unspec; hints.ai_socktype = sock_stream; hints.ai_protocol = ipproto_tcp; // resolve server address , port iresult = getaddrinfo("www.google.com", default_port, &hints, &result); if (iresult != 0) { printf("getaddrinfo failed error: %d\n", iresult); wsacleanup(); return 1; } // attempt connect address until 1 succeeds (ptr = result; ptr != null; ptr = ptr->ai_next) { // create socket connecting server connectsocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (connectsocket == invalid_socket) { printf("socket failed error: %ld\n", wsagetlasterror()); wsacleanup(); return 1; } // connect server. iresult = connect(connectsocket, ptr->ai_addr, (int)ptr->ai_addrlen); if (iresult == socket_error) { closesocket(connectsocket); connectsocket = invalid_socket; continue; } break; } freeaddrinfo(result); if (connectsocket == invalid_socket) { printf("unable connect server!\n"); wsacleanup(); return 1; } // send initial buffer iresult = send( connectsocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iresult == socket_error) { printf("send failed error: %d\n", wsagetlasterror()); closesocket(connectsocket); wsacleanup(); return 1; } printf("bytes sent: %ld\n", iresult); // shutdown connection since no more data sent iresult = shutdown(connectsocket, sd_send); if (iresult == socket_error) { printf("shutdown failed error: %d\n", wsagetlasterror()); closesocket(connectsocket); wsacleanup(); return 1; } // receive until peer closes connection /* { iresult = recv(connectsocket, recvbuf, recvbuflen, 0); if ( iresult > 0 ) printf("bytes received: %d\n", iresult); else if ( iresult == 0 ) printf("connection closed\n"); else printf("recv failed error: %d\n", wsagetlasterror()); } while( iresult > 0 ); */ // cleanup closesocket(connectsocket); wsacleanup(); return 0; }
if change buffer "this test" valid http request, can response can read (i comment part away). not necessary connect tcp/ip says connection possible. there of course udp ping ways, have (in opinion) no advantage. name resolving can dropped , can use ip directly. name resolving no sign of working internet provider have internal dns server can if connection internet not possible.
Comments
Post a Comment