C# IP地址和DNS 网络

Posted 指间的徘徊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# IP地址和DNS 网络相关的知识,希望对你有一定的参考价值。

 1             Uri i = new Uri("http://www.baidu.com");   //可获取属性
 2 
 3             UriBuilder u = new UriBuilder("http://www.baidu.com");  //可获取、设置属性
 4 
 5             IPAddress ipAddress = IPAddress.Parse("192.168.31.34");  //ip地址转换
 6             byte[] address = ipAddress.GetAddressBytes();
 7             string ipString = ipAddress.ToString();
 8 
 9             IPHostEntry host = Dns.Resolve("www.baidu.com"); //获取IPHostEntry对象
10             IPHostEntry whost = Dns.GetHostByAddress("192.168.31.114");

根据域名、地址,查询主机信息

Form

 1 public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             try
11             {
12                 IPHostEntry iphost = Dns.GetHostEntry(textBox1.Text);
13                 foreach(var ip in iphost.AddressList)
14                 {
15                     string ipaddress = ip.AddressFamily.ToString();
16                     listBox1.Items.Add(ipaddress);
17                     listBox1.Items.Add("  " + ip.ToString());
18                 }
19                 textBox2.Text = iphost.HostName;
20 
21 
22             }
23             catch (Exception ex)
24             {
25                 MessageBox.Show("Unable to process  the request because the following problem occurred:\\n" + ex.Message + " Exception occurred");   
26              
27             }
28         }
29     }

 

Designer

  1    partial class Form1
  2     {
  3         /// <summary>
  4         /// Required designer variable.
  5         /// </summary>
  6         private System.ComponentModel.IContainer components = null;
  7 
  8         /// <summary>
  9         /// Clean up any resources being used.
 10         /// </summary>
 11         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 12         protected override void Dispose(bool disposing)
 13         {
 14             if (disposing && (components != null))
 15             {
 16                 components.Dispose();
 17             }
 18             base.Dispose(disposing);
 19         }
 20 
 21         #region Windows Form Designer generated code
 22 
 23         /// <summary>
 24         /// Required method for Designer support - do not modify
 25         /// the contents of this method with the code editor.
 26         /// </summary>
 27         private void InitializeComponent()
 28         {
 29             this.label1 = new System.Windows.Forms.Label();
 30             this.button1 = new System.Windows.Forms.Button();
 31             this.textBox1 = new System.Windows.Forms.TextBox();
 32             this.textBox2 = new System.Windows.Forms.TextBox();
 33             this.label2 = new System.Windows.Forms.Label();
 34             this.label3 = new System.Windows.Forms.Label();
 35             this.listBox1 = new System.Windows.Forms.ListBox();
 36             this.SuspendLayout();
 37             // 
 38             // label1
 39             // 
 40             this.label1.AutoSize = true;
 41             this.label1.Location = new System.Drawing.Point(12, 18);
 42             this.label1.Name = "label1";
 43             this.label1.Size = new System.Drawing.Size(281, 12);
 44             this.label1.TabIndex = 0;
 45             this.label1.Text = "Enter name to resolve and click Resolve button";
 46             // 
 47             // button1
 48             // 
 49             this.button1.Location = new System.Drawing.Point(259, 30);
 50             this.button1.Name = "button1";
 51             this.button1.Size = new System.Drawing.Size(75, 21);
 52             this.button1.TabIndex = 1;
 53             this.button1.Text = "Resolve";
 54             this.button1.UseVisualStyleBackColor = true;
 55             this.button1.Click += new System.EventHandler(this.button1_Click);
 56             // 
 57             // textBox1
 58             // 
 59             this.textBox1.Location = new System.Drawing.Point(15, 33);
 60             this.textBox1.Name = "textBox1";
 61             this.textBox1.Size = new System.Drawing.Size(228, 21);
 62             this.textBox1.TabIndex = 2;
 63             // 
 64             // textBox2
 65             // 
 66             this.textBox2.Location = new System.Drawing.Point(15, 76);
 67             this.textBox2.Name = "textBox2";
 68             this.textBox2.Size = new System.Drawing.Size(228, 21);
 69             this.textBox2.TabIndex = 3;
 70             // 
 71             // label2
 72             // 
 73             this.label2.AutoSize = true;
 74             this.label2.Location = new System.Drawing.Point(12, 61);
 75             this.label2.Name = "label2";
 76             this.label2.Size = new System.Drawing.Size(53, 12);
 77             this.label2.TabIndex = 4;
 78             this.label2.Text = "HostName";
 79             // 
 80             // label3
 81             // 
 82             this.label3.AutoSize = true;
 83             this.label3.Location = new System.Drawing.Point(12, 108);
 84             this.label3.Name = "label3";
 85             this.label3.Size = new System.Drawing.Size(77, 12);
 86             this.label3.TabIndex = 5;
 87             this.label3.Text = "IP Addresses";
 88             // 
 89             // listBox1
 90             // 
 91             this.listBox1.FormattingEnabled = true;
 92             this.listBox1.ItemHeight = 12;
 93             this.listBox1.Location = new System.Drawing.Point(15, 123);
 94             this.listBox1.Name = "listBox1";
 95             this.listBox1.Size = new System.Drawing.Size(217, 160);
 96             this.listBox1.TabIndex = 6;
 97             // 
 98             // Form1
 99             // 
100             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
101             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
102             this.ClientSize = new System.Drawing.Size(346, 333);
103             this.Controls.Add(this.listBox1);
104             this.Controls.Add(this.label3);
105             this.Controls.Add(this.label2);
106             this.Controls.Add(this.textBox2);
107             this.Controls.Add(this.textBox1);
108             this.Controls.Add(this.button1);
109             this.Controls.Add(this.label1);
110             this.Name = "Form1";
111             this.Text = "Form1";
112             this.ResumeLayout(false);
113             this.PerformLayout();
114 
115         }
116 
117         #endregion
118 
119         private System.Windows.Forms.Label label1;
120         private System.Windows.Forms.Button button1;
121         private System.Windows.Forms.TextBox textBox1;
122         private System.Windows.Forms.TextBox textBox2;
123         private System.Windows.Forms.Label label2;
124         private System.Windows.Forms.Label label3;
125         private System.Windows.Forms.ListBox listBox1;
126     }
View Code

 

以上是关于C# IP地址和DNS 网络的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 代码中获取 IP 地址 [重复]

简述IP地址,DNS.和URL之间的关系

Centos7网络配置( 网关、dns、ip地址配置)

无法显示此网页出现错误代码“DNS-PROBE-POSSIBLE”怎么解决?

C# asp.net获取客户端的Ipv4 ip地址[重复]

https网络编程——DNS域名解析获取IP地址