C # network programming Overview 1

Source: Internet
Author: User
Tags dns entry
From http://www.cnblogs.com/xh831213/archive/2006/02/13/329624.html Code
C # As a language that gathers the expertise of many people, it has great advantages in various aspects, especially network programming. This article will introduce some basic knowledge and methods for network programming with C.

Microsoft's. NET Framework provides two namespaces for network programming: system. NET and system. net. sockets. By reasonably using the classes and methods, we can easily compile various network applications.Program. 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 introduce some classes in C # network programming: DNS, iphostentry, ipendpoint, and socket classes. Finally, I will provide corresponding instances to help readers better understand them.

DNS class:

To use TCP/IP Internet service applications provide domain name services. Its resolve () method queries DNS servers to convert user-friendly Domain Names (such"Www.google.com") Maps to a digital Internet address (for example192.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 StaticIphostentry resolve (StringHostname );

The followingCodeObtain an IPaddress instance that contains the IP address of the server www.google.com:

Iphostentry iphostinfo=DNS. Resolve ("Www.google.com");

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 StaticIphostentry gethostbyaddress (StringIPaddress );

Public StaticIphostentry gethostbyname (StringHostname );

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(IntI= 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.

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.