Narzędzia użytkownika

Narzędzia witryny


Pasek boczny

sk2:sockets_netdbs

To jest stara wersja strony!


Zmiana adresu domenowego na IP i z powrotem

Tradycyjna funkcja ''gethostbyname''

Najstarsze funkcje do zmiany to gethostbyname i gethostbyaddr. Funkcje są przestarzałe i powszechnie używane. Problemy z ich działaniem obrazuje poniższy kod:

ghbn.cpp
#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>
#include <thread>
#include <atomic>
#include <unistd.h>
 
using namespace std;
 
void print(hostent* ret){
	cout << ret->h_name << endl;
	for(auto it = ret->h_addr_list; *it; ++it){
			cout << "  " << inet_ntoa(*((in_addr*)*it)) << endl;
	}
}
 
int main(int argc, char **argv) {
	atomic<bool> wait1 {true}, wait2 {true};
 
	std::thread 
	t1([&]{
		while(wait1.load());
		gethostbyname("spam.org");
		wait2.store(false);
		sleep(1);
	}),
	t2([&]{
		hostent* ret = gethostbyname("fc.put.poznan.pl");
		wait1.store(false);
		while(wait2.load());
		print(ret);
	});
 
	t1.join();
	t2.join();
	return 0;
}
sk2/sockets_netdbs.1444039518.txt.gz · ostatnio zmienione: 2015/10/05 12:05 przez jkonczak