It is common for networked programs to need to translate text-based representatives of an address or hostname into an address structure required by the socket programming API. The common function we've been using is getaddrinfo()
. It is a useful function because it is highly portable (available on Windows, Linux, and macOS), and it works for both IPv4 and IPv6 addresses.
It is also common to need to convert a binary address back into a text format. We use getnameinfo()
for this.
Although we've been using getaddrinfo()
in previous chapters, we'll discuss it in more detail here.
The declaration for getaddrinfo()
is shown in the following code:
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
The preceding code snippet is explained as follows:
node
specifies a hostname or address as a string. Valid examples could beexample.com
,192.168...