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 .....