". NET 4.0 getting started with network development"-What is IP address (I)

Source: Internet
Author: User

This is a series of articles 《. the expanded reading of the book "NET 4.0 Object-Oriented Programming mantalk". During the writing process, I assume that readers can read the relevant sections of this book without wasting any effort on repeatedly introducing relevant content.

For other types of readers, unless you have the corresponding. NET technical background and certain development experience, you may encounter difficulties in reading.

I hope this series of articles will give readers a taste of the charm of network development!

In addition, these articles are original articles. Please respect the work of the author. I allow everyone to freely repost these articles and related examples for the purpose of knowledge sharing, but without my permission, please do not use it for commercial profit purposes.

Thank you!

Jin xuliang

========================================================== ===

 


Click the following link to read the previous articles in this series:

 

The article ". NET 4.0 getting started with network development series"-No network wins 


========================================

 

 

The Internet is connected to non-counting computers. How to distinguish them is a matter that must be carefully considered, just as people use "names" for distinction, every computer directly connected to the Internet must also have a unique identifier. Otherwise, how can we find the expected receiver of the information transmitted on the Internet?

 


1. IPv4 and IPv6

 


IP addresses are a method used to distinguish between computers directly connected to the Internet. Currently, IPv4 is widely used to identify Internet hosts using four bytes. For example, Baidu's IP address is: 61.135.169.105: two computers directly connected to the Internet cannot have the same IP address. Otherwise, the Internet will be messy, which is easy to understand.

The biggest problem with IPv4 is the limited number of addresses, and the Internet requires that all connected hosts have different IP addresses. In the era of explosive growth of various information devices connected to the Internet, the Address is not enough soon. To save money, people have come up with a variety of workarounds, the most common one is NAT (Network Address Translation), a computer in a sub-Network, only a few hosts (usually only one) can directly connect to the Internet. IP addresses of hosts in different subnets can be repeated. When computers in the subnet need to access the Internet, they need to be "forwarded" by computers directly connected to the Internet in the subnetwork ".

Although it seems that NAT is good, it can solve the problem of insufficient IPv4 addresses, but it also has its disadvantages: it is very convenient for Intranet hosts to access the Internet. When hosts on the internet need to access Intranet computers, it will be troublesome, because the IP address of an Intranet computer is not a "real" Internet address, the connection between the two computers must be implemented by other means (for example, the Intranet address translation of another dedicated computer ), very troublesome.

The final solution is IPv6. It expands the address to 128 bits and uses a hexadecimal representation. An example is as follows:

 


2001: db8: 2000: 240: 290: 27ff: fe24: c19f/64

 


The last "/64" indicates that the first 64 is the network ID, and the remaining 64-bit (128-64 = 64) is the host ID.

IPv6 provides a "massive" address space. With it, all connected devices can have a "global" (even "whole solar system" and "whole galaxy ") A unique address. NAT is no longer needed.

Although IPv6 is good, IPv4 is still the mainstream, and a large number of network applications use IPv4. I don't know how long it will take for IPv6 to completely send IPv4 to the museum of technology development history.

In fact, in network development, besides IPv4 and IPv6, there are other types of addresses .. The NET base class library provides an AddressFamily enumeration to list these "Address type families ":

 

 

Public enum AddressFamily

{

Unknown =-1, // Unknown address family.

Unspecified = 0, // Unspecified address family.

InterNetwork = 2, // Address for IP version 4.

InterNetworkV6 = 23, // Address for IP version 6.

//......

}

AddressFamily defines a "Heap" enumeration value. Many address types are not heard by most people. They are mostly used in specific fields, and some address types are "historical issues ", it appeared in the early development history of computer networks and is rarely used now. Let's leave it to our friends who have an "archaeological" hobby to explore it. In. NET development, what we actually use is AddressFamily. InterNetwork and AddressFamily. InterNetworkV6, which represent IPv4 and IPv6 respectively.

2. Use IP addresses in. NET Development

The. NET base class library uses IPAddress to represent an IP address (Figure 1 ):

Figure 1

As shown in 1, IPAddress defines some fields. These fields represent special IP addresses. Their values are as follows:

IPAddress. Any: 0.0.0.0

IPAddress. IPv6Any :::

IPAddress. IPv6Loopback: 1

IPAddress. Loopback: 127.0.0.1

IPAddress. None: 255.255.255.255

IPAddress. IPv6None :::

IPAddress. Broadcast: 255.255.255.255

Brief explanation:

When a Socket uses an address with the word "Any", It listens to all Network interfaces (Network interfaces) on the host ). All bits in the "Any" address are 0, and IPv6 is abbreviated as ":". In IPv6, ":" is a series of 0 compression notation.

In actual development, we can Bind a socket to an "Any" address. When the program runs, windows will automatically bind it to a suitable local IP address.

Correspondingly, the socket does not listen to any network interface if it contains a "None" address.

Note that the "None" address conventions of IPv4 and IPv6 are different. IPAddress. the value of IPv6None and IPAddress. the value of IPv6Any is the same (all bits are 0), but they are two different IPAddress objects. The output result is actually obtained by calling the ToString () method of IPAddress.

After obtaining an IPAddress object, you can call its toString method to obtain its classic representation (such as "127.0.0.1"), or call the GetAddressBytes method to obtain its hexadecimal representation:

 

 

// Output the classic and hexadecimal IP address formats

Public static void PrintIPAddress (IPAddress ipAddress)

{

Console. WriteLine (ipAddress. ToString ());

Byte [] bytes = ipAddress. GetAddressBytes ();

Console. WriteLine (BitConverter. ToString (bytes ));

}

In the above Code, BitConverter should note that it can easily perform various operations on Byte arrays and is useful in network applications.

The values of IPAddress. None and IPAddress. Broadcast are "Broadcast bandwidth 255.255", indicating that all IP addresses are 1. This address usually represents "Broadcast", indicating that data is sent to all devices on the Internet. Obviously, this is just an "Fantasy". Generally, routers do not forward such data packets. Otherwise, some "bad guys" constantly "broadcast data packets, and the entire Internet will be like Beijing, become "first block" and "Global Wait (WWW: World Wide Wait )".

The most interesting ones are IPAddress. Loopback and IPAddress. IPv6Loopback. We usually call them "Loopback Interfaces". This is a virtual "NIC" that represents the local machine. Packets sent to the "loopback interface" are not actually sent to the Internet, but are directly "digested internally" to the local host. On the Windows platform, there is also a "." common "localhost" in Web and network development, which corresponds to this loop interface, representing the local host. For example, we can use ". SqlExpress" to access the SQL Server database installed on the local machine.

 

====================================

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.