Hálózati kapcsolat meglétének ellenőrzése C++-ban

Az alábbi kód segítségével megállapítható, hogy az operációs rendszer rendelkezik-e élő hálózati kapcsolattal (adott esetben Internet eléréssel). A megoldásban a trükk, hogy sorra vesszük az elérhető hálózati csatolókat és megvizsgáljuk, tartozik-e hozzájuk érvényes IP cím. Ez a megoldás csak dinamikus IP-kiosztás esetén működik, mivel fix IP cím esetén a hálózati interfész minden körülménytől függetlenül rendelkezik a megadott IP címmel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <list>
 
using namespace std;
 
bool network_connected (list<string> &ifaces) {
	struct ifaddrs *if_addr_struct = NULL;
	struct ifaddrs *if_addr = NULL;
	void *addr_pointer = NULL;
	bool ip_found = false;
 
	ifaces.clear();
	getifaddrs (&if_addr_struct);
 
	for (if_addr = if_addr_struct; if_addr != NULL; if_addr = if_addr->ifa_next) {
		if (if_addr->ifa_addr->sa_family == AF_INET) {
			addr_pointer = &((struct sockaddr_in *) if_addr->ifa_addr)->sin_addr;
			char addressBuffer[INET_ADDRSTRLEN];
			inet_ntop (AF_INET, addr_pointer, addressBuffer, INET_ADDRSTRLEN);
			if (strcmp (if_addr->ifa_name, "lo") != 0 && strcmp (addressBuffer, "127.0.0.1") != 0) {
				ip_found = true;
				ifaces.push_back (string (if_addr->ifa_name));
			}
		}
	}
 
	if (if_addr_struct != NULL) {
		freeifaddrs (if_addr_struct);
	}
 
	return ip_found;
}
 
int main() {
	list<string> ifaces;
 
	for (int i = 0; i < 3; i++) {
		if (network_connected (ifaces)) {
			cout << "Connected";
			for (list<string>::const_iterator it = ifaces.begin(); it != ifaces.end(); it++) {
				cout << " " << *it;
			}
			cout << endl;
		}
		else {
			cout << "NOT connected" << endl;
		}
 
		sleep (1);
	}
}

A 28. sorban a lo hosztnevet és a 127.0.0.1 IP-t kivételként kezeljük, mivel ezek csak a visszacsatoló (loopback) interfészek.

WordPress › hiba

Súlyos hiba történt a webhelyünkön.

További információ a WordPress hibakeresésről.