C # Network Programming

Source: Internet
Author: User

It's boring today. I don't know what to do! I have been doing enterprise development before. I recently read some articles on the Internet and I am also interested in network programming. So I started to learn network programming, after learning some basic programs today, I think of writing an article to record what I have learned. Just take a note in the garden.

When talking about networks, many friends may think of concepts related to IP, TCP, UDP, IP, MAX, DNS, and so on. These words are indeed very important in network development, this article will also be written with these keywords.

1. Get the Host Name
The Dns class is located under System. Net, and there is a special method GetHostName () under this class for obtaining the computer name. We can use this method to obtain the computer name as follows:

String hostName = Dns. GetHostName ();

2. Get IP Address Settings
The host name is obtained above. Here, we can use the host information container class IPHostEntry to obtain the IP settings (based on the host name ).
IPHostEntry myself = Dns. GetHostByName (hostName );
In this way, an address set is obtained. We can obtain specific information through iteration of this set.

Foreach (IPAddress address in myself. AddressList)
{
Console. WriteLine ("IP Address: {0}", address. ToString ());
}

Iii. instance code

Public static void Main ()
{
String hostName = Dns. GetHostName ();
Console. WriteLine ("Local hostname: {0}", hostName );
IPHostEntry myself = Dns. GetHostByName (hostName );
Foreach (IPAddress address in myself. AddressList)
{
Console. WriteLine ("IP Address: {0}", address. ToString ());
}
}

Iv. instance running result

Zhou Gong called me and wrote it next time .....

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.