Notes when using IPaddress to resolve IP addresses

Source: Internet
Author: User
Because. Net does not have controls such as the IP input box, we can only input and parse IP addresses .. NET provides us with a convenient IP address processing class: IPaddress, which can be used not only for IPv4, but also for IPv6 IP addresses. Besides. net, Winsock2 also provides the inet_addr function for us to resolve IP addresses.

However, when we use IPaddress. when parse or inet_addr function is used to resolve the IP address, the problem occurs: Enter "192.168.0.1". But if you enter "192.168.0.09", an error is returned, while "192.168.0.01" is correct! I guess the error here is that during system parsing, "09" is treated as a gossip ( Program In October 0), of course, there is no "09" in the October! In fact, in our usual habits, for example, when setting the IP address of the NIC, adding a few zeros before a certain number of IP addresses is completely acceptable, and the system will automatically ignore the excess 0.

This problem is not mentioned in the IPaddress. parse function of. Net in msdn, but the inet_addr function provides a detailed description. For more information, see:
Internet ses sses

values specified using ". ''notation take one of the following forms:
. b. c. d. b. c. B A
when four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the 4 bytes of an Internet address. when an Internet address is viewed as a 32-bit integer quantity on the intel architecture, the bytes referred to abve appear as "D. c. b. A ''. that is, the bytes on an Intel processor are ordered from right to left.
the parts that make up an address in ". "notation can be decimal, octal or hexadecimal as specified in the C language. numbers that start with "0x" or "0x" imply hexadecimal. numbers that start with "0" imply octal. all other numbers are interpreted as decimal.

Internet address value meaning
"4.3.2.16" decimal
"004.003.002.020" octal
"0x4.0x3.0x2.0x10" hexadecimal
"4.003.002.0x10" mix

NoteThe following notations are only used by Berkeley, and nowhere else on the Internet. For compatibility with their software, they are supported as specified.

When a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right-most 2 bytes of the network address. this makes the three-part address format convenient for specifying Class B network addresses as "128.net. host''

When a two-part address is specified, the last part is interpreted as a 24-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. host ''.

When only one part is given, the value is stored directly in the network address without any byte rearrangement.

For self-compiled IP address input and resolution, you must note that the value is 0 in front of the number! So use IPaddress. when parse is used, you must be careful when dealing with the extra 0 values in the string. Otherwise, it will bring a lot of confusion to the user: the input is "192.168.0.010". How to parse and save it into "192.168.0.8.

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.