Construct a webpage download server in the. NET Environment (1)

Source: Internet
Author: User
Tags dns entry

I. Preface:

Microsoft's. Net Framework provides two namespaces for network programming: System. Net and System. Net. Sockets. By properly using the classes and methods, we can easily compile various network applications. Such network applications can be based on both stream sockets and datagram sockets. TCP is the most widely used protocol in stream socket-based communication. UDP is the most widely used protocol in datagram socket-based communication. Next I will focus on introducing it to you. NET network programming class: Dns class, IPHostEntry class, IPEndPoint class, and Socket class, and a web page download server instance will be provided at the end to deepen the reader's understanding. NET network programming.

Ii. network programming class introduction:

Dns class:

Provides domain name services to applications that use TCP/IP Internet services. Its Resolve () method queries DNS servers to map user-friendly Domain Names (such as "www.google.com") to digital Internet addresses (such as 192.168.1.1 ). The Resolve () method returns an IPHostEnty instance that contains a list of the addresses and aliases of the requested names. In most cases, you can use the first address returned in the AddressList array.

The function prototype of the Resolve () method is as follows:

Public static IPHostEntry Resolve (string hostName );



The following code gets an IPAddress instance that contains the IP address of the server www.google.com:

IPHostEntry ipHostInfo = Dns. Resolve ("www.google.com ");
IPAddress ipAddress = ipHostInfo. AddressList [0];



However, in the Dns class, in addition to the Resolve () method, you can also use the GetHostByAddress () method and GetHostByName () method to obtain the corresponding IPHostEntry instance. The function prototype is as follows:

Public static IPHostEntry GetHostByAddress (string IPAddress );
Public static IPHostEntry GetHostByName (string hostName );



The following code shows how to use the above two methods to obtain IPHostEntry instances that contain information about the server www.google.com:

IPHostEntry hostInfo = Dns. GetHostByAddress ("192.168.1.1 ");
IPHostEntry hostInfo = Dns. GetHostByName ("www.google.com ");



When using the preceding methods, you may need to handle the following exceptions:

SocketException exception: the operating system error occurs when the Socket is accessed.

ArgumentNullException exception: the parameter is null.

ObjectDisposedException exception: the Socket has been closed.

As mentioned above, I briefly introduced some methods and their usage in the Dns class, and listed some possible exceptions, next let's go to the IPHostEntry class closely related to the Dns class.

IPHostEntry class:

This type of instance object contains the address information of the Internet host. All public static members of this type are secure for multi-threaded operations, but they are not guaranteed to be thread-safe for any instance members. The main attributes include AddressList, Aliases, and HostName.

The AddressList attribute and the Aliases attribute are used to obtain or set the list of IP addresses associated with the host and the list of Aliases associated with the host. The AddressList attribute value is an array of the IPAddress type, including the IP address resolved to the host name included in the Aliases attribute; the Aliases attribute value is a set of strings that contain the DNS name resolved to the IP address in the AddressList attribute. The HostName attribute is easy to understand. It contains the main host name of the server, which can be understood simply by the name. If the DNS entry of the server defines additional Aliases, you can use these Aliases in the Aliases attribute.

The following code lists the related aliases of the server www.google.com and the length of the IP address list, and lists all IP addresses:

IPHostEntry IPHost = Dns. Resolve ("www.google.com /");
String [] aliases = IPHost. Aliases;
Console. WriteLine (aliases. Length );

IPAddress [] addr = IPHost. AddressList;
Console. WriteLine (addr. Length );
For (int I = 0; I <addr. Length; I ++)
{
Console. WriteLine (addr [I]);
}



After the IPHostEntry class is introduced, we can obtain the IP address and Alias List of the host to be connected. However, an important class-IPEndPoint class is required to connect to the host.

There are two pages in this news. Currently, there are two pages in page 1st.


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.