#include #include #include #include // ┐ getaddrinfo jest #pragma comment(lib, "Ws2_32.lib") // ┘ w innej bibliotece // poniższy blok kodu, do funkcji main, to pomocnicze struktury i funkcje; // proszę w trakcie zajęć nie poświęcać czasu na ich analizę! using namespace std; typedef unordered_map map; map families{{AF_INET,"AF_INET "},{AF_INET6,"AF_INET6"}}; map socktypes{{0,"0"},{SOCK_RAW,"SOCK_RAW "},{SOCK_STREAM,"SOCK_STREAM"},{SOCK_DGRAM,"SOCK_DGRAM "}}; map protocols{{0,"0"},{IPPROTO_TCP,"IPPROTO_TCP"},{IPPROTO_UDP,"IPPROTO_UDP"}}; string getIp(sockaddr*a,socklen_t l){char b[40];getnameinfo(a,l,b,40,0,0,NI_NUMERICHOST);return b;} string getPort(sockaddr*a,socklen_t l){char b[6];getnameinfo(a,l,0,0,b,6,NI_NUMERICSERV);return b;} int main(int c, char**a) { WSADATA wsadata{}; // ┐ no i standardowo trzeba WSAStartup(MAKEWORD(2, 2),&wsadata); // ┘ zainicjalizować winsock const char *host = (c<2 ? "ietf.org" : a[1]); const char *port = (c<3 ? nullptr : a[2]); addrinfo *resolved; int res = getaddrinfo(host, port, nullptr, &resolved); if (res) { cerr << "Getaddrinfo failed: " << gai_strerror(res) << endl; return 1; } for (addrinfo *it = resolved; it; it = it->ai_next) { cout << "family: " << families[ it->ai_family] << " address: " << getIp( it->ai_addr, it->ai_addrlen) << " port: " << getPort( it->ai_addr, it->ai_addrlen) << " socktype: " << socktypes[it->ai_socktype] << " protocol: " << protocols[it->ai_protocol] << endl; } freeaddrinfo(resolved); return 0; }