Dydaktyka:
To jest stara wersja strony!
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:
#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; }