C#根據域名獲取IP地址
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
C#根據域名獲取IP地址,就像網絡上的IP查詢工具那樣,輸入域名即可查詢到對應的IP地址,需要連接網絡才行,同樣是根據DNS查詢IP地址,在Windows中,Ping命令可以實現此功能,但是不是調用了Ping的內核這個還不確定,有興趣的自己下載源代碼研究。
class ResolveDNS { IPAddress[] m_arrayIPs; public void Resolve(string s_host) { IPHostEntry hostInfo; try { hostInfo = Dns.GetHostByName(s_host); m_arrayIPs = hostInfo.AddressList; } catch(ArgumentNullException e) { MessageBox.Show("Source : " + e.Source + " Message : " + e.Message); } catch(Exception e) { MessageBox.Show("Source : " + e.Source + " Message : " + e.Message); } } public IPAddress this[int nIndex] { get { return m_arrayIPs[nIndex]; } } public int IPLength { get { return m_arrayIPs.Length; } } } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new GetIPofDN()); } private void btnOK_Click(object sender, System.EventArgs e) { lstIPList.Items.Clear(); ResolveDNS resolver1 = new ResolveDNS(); resolver1.Resolve(txtDN.Text); lblIPList.Text = "The IP address list of " + txtDN.Text + " are: "; int n = resolver1.IPLength; for( int i=0; i<n; i++ ) lstIPList.Items.Add(resolver1[i]); } 附件:GetIP.rar 該文章在 2021/2/4 17:19:42 編輯過 |
關鍵字查詢
相關文章
正在查詢... |