Examples › Socket Examples
socket_hostinfo
Legacy: examples_socket/hostinfo/testprog.cpp · Modern: examples_modern/socket/hostinfo/modern.cpp
Modern equivalent of the host name information example. The legacy program uses gethostbyname() through GetHostInformation() and must delete the result. getaddrinfo() is the modern call: it returns IPv4 and IPv6 addresses, is thread safe, and its list is freed by freeaddrinfo(), here owned by a std::unique_ptr with a custom deleter so it is freed on every return path. Addresses are printed with inet_ntop() into a std::array. (getaddrinfo is POSIX and Winsock 2; gxSocket is only used to start the socket library on Windows.)
Usage: modern [HOSTNAME] (default: localhost)
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| gxSocket | keep gxSocket (C++ has no sockets); servers in std::jthread with std::stop_token and select() timeouts | C++20 | Stopping is a request, not a canceled thread blocked in accept(). |
| GetHostInformation(), gethostbyname() | getaddrinfo() owned by std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> | C++11 + POSIX/Winsock 2 | IPv4 and IPv6, thread safe. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_socket/hostinfo/testprog.cpp 69 lines
#include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include <iostream> using namespace std; // Use unqualified names for Standard C++ library #else // Use the old iostream library by default #include <iostream.h> #endif // __USE_ANSI_CPP__ #include "gxsocket.h" #ifdef __MSVC_DEBUG__ #include "leaktest.h" #endif int main(int argc, char **argv) { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif // Check the command line arguments if(argc != 2) { cout << "\n"; cout << "Usage: " << argv[0] << " hostname" << "\n"; return 1; } char *hostname = argv[1]; gxSocket sock_lib; if(sock_lib.InitSocketLibrary() != 0) { cout << sock_lib.SocketExceptionMessage() << "\n"; return 1; } gxsInternetAddress *ialist; gxsHostNameInfo *hostnm = sock_lib.GetHostInformation(hostname); if(!hostnm) { cout << "Socket exception: Could not resolve hostname" << "\n"; return 1; } cout << "\n"; cout << "Hostname: " << hostnm->h_name << "\n"; int i; for(i = 0; ; i++) { char *alias = hostnm->h_aliases[i]; if(alias == 0) break; cout << "Host Alias: " << alias << "\n"; } for(i = 0; ; i++) { ialist = (gxsInternetAddress *)hostnm->h_addr_list[i]; if(ialist == 0) break; cout << "Host IP Address: " << inet_ntoa(*ialist) << "\n"; } // Free the memory allocated for the gxsHostNameInfo data structure delete hostnm; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++17)
examples_modern/socket/hostinfo/modern.cpp 49 lines
#include <array> #include <iostream> #include <memory> #include <string> #if defined(_WIN32) #include <winsock2.h> #include <ws2tcpip.h> #else #include <arpa/inet.h> #include <netdb.h> #include <sys/socket.h> #endif #include "gxsocket.h" int main(int argc, char **argv) { gxSocket lib; if(lib.InitSocketLibrary() != 0) return 1; const std::string host = argc > 1 ? argv[1] : "localhost"; addrinfo hints{}; hints.ai_family = AF_UNSPEC; // IPv4 and IPv6 hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME; addrinfo *raw = nullptr; if(int rv = getaddrinfo(host.c_str(), nullptr, &hints, &raw); rv != 0) { std::cout << "Cannot resolve " << host << ": " << gai_strerror(rv) << "\n"; return 1; } std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> list(raw, &freeaddrinfo); std::cout << "Host name: " << (list->ai_canonname ? list->ai_canonname : host) << "\n"; for(const addrinfo *ai = list.get(); ai; ai = ai->ai_next) { std::array<char, INET6_ADDRSTRLEN> text{}; const void *addr = ai->ai_family == AF_INET ? static_cast<const void *>(&reinterpret_cast<const sockaddr_in *>(ai->ai_addr)->sin_addr) : static_cast<const void *>(&reinterpret_cast<const sockaddr_in6 *>(ai->ai_addr)->sin6_addr); inet_ntop(ai->ai_family, addr, text.data(), text.size()); std::cout << (ai->ai_family == AF_INET ? "IPv4 address: " : "IPv6 address: ") << text.data() << "\n"; } lib.ReleaseSocketLibrary(); return 0; // ~unique_ptr frees the list } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17 cmake --build build-modern --target socket_hostinfo ./build-modern/socket_hostinfo
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Host name: localhost IPv6 address: ::1 IPv4 address: 127.0.0.1