Windows socket programming--ip address translation

Source: Internet
Author: User
Tags rfc

In Windows socket programming, you need to convert the IP address between the network byte order and the host byte order, the code for the procedure is as follows:

1#include <Winsock2.h>2#include <Ws2tcpip.h>//in order to use the Inet_pton () and Inet_ntop () functions3#include <iostream>4 5 #pragmaComment (lib, "Ws2_32.lib")//socket programming requires referencing the library6 7 usingstd::cout;8 usingStd::endl;9 Ten intMain () { One sockaddr_in addr; A     Charip_buffer[ -]; -     //using the TCP/IP protocol -Inet_pton (Af_inet,"127.0.0.0", &addr); theInet_ntop (Af_inet, &addr, Ip_buffer, -); -cout << Ip_buffer <<Endl; -System"Pause"); -     return 0; +}

where Inet_pton () and Inet_ntop () are new functions that require more than one vista of the system to use, for IPv4 and IPv6, to replace the old functions such as inet_addr () and Inet_ntoa (), which are described in MSDN for these two functions:

Inetpton function

The Inetpton function converts an IPV4 or IPv6 Internet Network address on its standard text presentation form in To its numeric binary form. The ANSI version of this function is Inet_pton.

Syntaxc++
int Wsaapi Inetpton (  _in_ int Family,  _in_ pctstr pszaddrstring,  _out_ PVOID paddrbuf);
Parameters
Family [In]

The address family.

Possible values for the address family is defined in the Ws2def.h header file. Note that the Ws2def.h header file was automatically included inWinsock2.h, and should never be used dire ctly. Note the values for the Af_ address family and Pf_ protocol family constants is identical (for example, af_inet< /c3> and pf_inet), so either constant can be used.

The values currently supported is af_inet and af_inet6.

Value meaning
af_inet
2

The Internet Protocol version 4 (IPV4) address family. When this parameter are specified, the  pszaddrstring  parameter must point to a text representation of a IPV4 address and the  paddrbuf  parameter Returns a pointer to an  in_addr   structure that represents the IPV4 address.

Af_inet6
23

The Internet Protocol version 6 (IPV6) address family. When this parameter was specified, the pszaddrstring parameter must point to a text representation of an IPV6 addr ESS and the paddrbuf parameter returns a pointer to an IN6_ADDR structure that represents the IPv6 Addre Ss.

pszaddrstring [In]

A Pointer to the NULL-terminated string, contains the text representation of the IP address to convert to Num Eric binary form.

When the Family parameter was af_inet, then the pszaddrstring parameter must point to a text rep Resentation of an IPV4 address in the standard dotted-decimal notation.

When the Family parameter are af_inet6, then the pszaddrstring parameter must point to a text re Presentation of an IPV6 address in the standard notation.

Paddrbuf [out]

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.

When the Family parameter was af_inet, this buffer should being large enough to hold an in_addr str Ucture.

When the Family parameter was af_inet6, this buffer should was large enough to hold an in6_addr s Tructure.

Return value

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's not a VA Lid IPv4 Dotted-decimal String or a valid IPV6 address string. Otherwise, a value of-1 is returned, and a specific error code can being retrieved by calling the WSAGetLastError F or extended error information.

If the function has a error, the extended error code returned by WSAGetLastError can be one of the following Val UEs.

Error Code meaning
Wsaeafnosupport

The address family specified in the family parameter are not supported. This error is returned if the Family parameter specified were not af_inet or af_inet6.

Wsaefault

The pszaddrstring or paddrbuf parameters is NULL or is not part of the user address Spa Ce.

Remarks

The Inetpton function is supported on Windows Vista and later.

The Inetpton function provides a protocol-independent conversion of the Internet network address in their standard t Ext 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. While the inet_addr function works with IPV4 address strings, the Inetptonfunction works with eithe R IPv4 or IPV6 address strings.

The ANSI version of this function was Inet_pton as defined in RFC 2553. For more information, see RFC 2553 available at the IETF website.

The Inetpton function does not require that the Windows Sockets DLL is loaded to perform conversion of a text str ing that represents an IP address to a numeric binary IP address.

If the Family parameter specified is af_inetand then the pszaddrstring parameter must point a Tex T string of an IPv4 address in dotted-decimal notation as in "192.168.16.0", a example of an IPv4 address in DOTTED-DECIM Al notation.

If the Family parameter specified is af_inet6and then the pszaddrstring parameter must point a te XT string of IPV6 address in Internet standard format. The basic string representation consists of 8 hexadecimal numbers separated by colons. A string of consecutive zero numbers is replaced with a double-colon. There can only is one double-colon in the string representation of the IPV6 address. The last of the represented in Ipv4-style dotted-octet notation if the address is a ipv4-compatible address.

When Unicode or _unicode are defined, Inetpton is defined to inetptonw, the Unicode version of this funct Ion. The pszaddrstring parameter is defined to the pcwstr data type.

When UNICODE or _unicode are not defined, Inetpton are defined to Inetptona, the ANSI version of this Func tion. The ANSI version of this function was always defined as Inet_pton. The pszaddrstring parameter is defined to the pcstr data type.

The IN_ADDR structure is defined in the Inaddr.h header file. The IN6_ADDR structure is defined in the In6addr.h header file.

On Windows vista and later, the  rtlipv4stringtoaddress  and  Rtlipv4stringtoaddressex  functions can be used to convert a text representation of an IPv4 address in Intern ET standard dotted-decimal notation to a numeric binary address represented as an  in_addr  stru Cture. On Windows vista and later, the  rtlipv6stringtoaddress  and  Rtlipv6stringtoaddressex  functions can be used to convert a string representation of a IPV6 address to a Nu Meric binary IPV6 address represented as an  in6_addr  structure. the  rtlipv6stringtoaddressex  function is more flexible since it also converts a string Representation of an IPV6 address the can include a scope ID and port in standard notation to a numeric binary form.

Windows 8.1 and Windows Server R2: The inetptonw function is supported for Windows Store a PPS on Windows 8.1, Windows Server R2, and later.

Requirements

Minimum Supported Client

Windows 8.1, Windows Vista [Desktop Apps | Windows Store Apps]

Minimum Supported Server

Windows Server (Desktop Apps | Windows Store Apps]

Header

Ws2tcpip.h

Library

Ws2_32.lib

Dll

Ws2_32.dll

Unicode and ANSI names

inetptonw (Unicode) and Inetptona or Inet_pton (ANSI)

Windows socket programming--ip address translation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.