Inet_aton windows example
All other numbers are interpreted as decimal. When a two-part address is specified, the last part is interpreted as a bit quantity and placed in the right-most 3 bytes of the network address. This makes the two-part address format convenient for specifying Class A network addresses as "net. When only one part is given, the value is stored directly in the network address without any byte rearrangement. Windows 8. Winsock Functions. Winsock Reference. The InetPton function is supported on Windows Vista and later which provides a protocol-independent conversion of an Internet network address in its standard text presentation form into its numeric binary form.
The InetPton function takes a text representation of an Internet address pointed to by the pszAddrString parameter and returns a pointer to the numeric binary IP address in the pAddrBuf parameter. A string of consecutive zero numbers may be replaced with a double-colon. The last 32 bits may be represented in IPv4-style dotted-octet notation if the address is an IPv4-compatible address. WSACleanup ;. Running on the older platform will generate a message similar to the following crap:. This section covers the legacy name resolution and is included only for the sake of code maintenance, because new applications should be using getaddrinfo and getnameinfo.
The other feature you will notice is that the two new API calls replace eight legacy functions. The cp field is a null-terminated character string that accepts an IP address in dotted notation. This function is declared as:. If the call. Build and run the project. The following screenshot shows a sample output. The first two functions translate a hostname to its network IPv4 addresses and the second two do the reverse - map an IPv4 network address back to a hostname.
A host can have more than one IP address assigned to it. Each address in the array is returned in network-byte order. Normally, applications use the first address in the array.
However, if more than one address is returned, applications should randomly choose an available address rather than always use the first address. The other members of the union are for if you want to manipulate the address somehow, so that you can look at it byte by byte. On Windows this makes sense because Windows is always little-endian.
You rarely need to do that, though: the most common manipulation I guess would be to apply a netmask, and you don't need byte access for that. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Note that the Ws2def. A pointer to the NULL -terminated string that contains the text representation of the IP address to convert to numeric binary form.
A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in network byte order. If no error occurs, the InetPton function returns a value of 1 and the buffer pointed to by the pAddrBuf parameter contains the binary numeric IP address in network byte order. The InetPton function returns a value of 0 if the pAddrBuf parameter points to a string that is not a valid IPv4 dotted-decimal string or a valid IPv6 address string.
Otherwise, a value of -1 is returned, and a specific error code can be retrieved by calling the WSAGetLastError for extended error information.
0コメント