C#根據(jù)域名獲取IP地址
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
C#根據(jù)域名獲取IP地址,就像網(wǎng)絡(luò)上的IP查詢工具那樣,輸入域名即可查詢到對(duì)應(yīng)的IP地址,需要連接網(wǎng)絡(luò)才行,同樣是根據(jù)DNS查詢IP地址,在Windows中,Ping命令可以實(shí)現(xiàn)此功能,但是不是調(diào)用了Ping的內(nèi)核這個(gè)還不確定,有興趣的自己下載源代碼研究。
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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |