根据IP地址获取主机名称
生活随笔
收集整理的這篇文章主要介紹了
根据IP地址获取主机名称
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IP地址獲得主機名稱
????????///?<summary>
????????///?根據IP地址獲得主機名稱
????????///?</summary>
????????///?<param?name="ip">主機的IP地址</param>
????????///?<returns>主機名稱</returns>
????????public?string?GetHostNameByIp(string?ip)
????????{
????????????ip?=?ip.Trim();
????????????if?(ip?==?string.Empty)
????????????????return?string.Empty;
????????????try
????????????{
????????????????//?是否?Ping?的通
????????????????if?(this.Ping(ip))
????????????????{
????????????????????System.Net.IPHostEntry?host?=?System.Net.Dns.GetHostEntry(ip);
????????????????????return?host.HostName;
????????????????}
????????????????else
????????????????????return?string.Empty;
????????????}
????????????catch?(Exception)
????????????{
????????????????return?string.Empty;
????????????}
????????}
說明:如果你的電腦可以上網你甚至可以查詢到:IP地址“64.233.189.104”是 Google 的一個名為“hk-in-f104.google.com”的主機的IP地址。
關于代碼中 this.Ping(ip) 方法后面再說。
既然說了如何“根據IP地址獲得主機名稱”,那就要再說說如何“根據主機名獲得主機的IP地址”吧。
2. 根據主機名獲得主機的IP地址
????????///?<summary>
????????///?根據主機名(域名)獲得主機的IP地址
????????///?</summary>
????????///?<param?name="hostName">主機名或域名</param>
????????///?<example>GetIPByDomain("pc001");?GetIPByDomain("www.google.com");</example>
????????///?<returns>主機的IP地址</returns>
????????public?string?GetIpByHostName(string?hostName)
????????{
????????????hostName?=?hostName.Trim();
????????????if?(hostName?==?string.Empty)
????????????????return?string.Empty;
????????????try
????????????{
????????????????System.Net.IPHostEntry?host?=?System.Net.Dns.GetHostEntry(hostName);
????????????????return?host.AddressList.GetValue(0).ToString();
????????????}
????????????catch?(Exception)
????????????{
????????????????return?string.Empty;
????????????}
????????}
說明:如果你的電腦可以上網你甚至可以查詢到:“www.google.com”的IP地址是“64.233.189.104”。
最后,再說說C#實現簡單的 Ping 的功能,用于測試網絡是否已經聯通。
3. C#實現簡單的 Ping 的功能,用于測試網絡是否已經聯通
????????///?<summary>
????????///?是否能?Ping?通指定的主機
????????///?</summary>
????????///?<param?name="ip">ip?地址或主機名或域名</param>
????????///?<returns>true?通,false?不通</returns>
????????public?bool?Ping(string?ip)
????????{
????????????System.Net.NetworkInformation.Ping?p?=?new?System.Net.NetworkInformation.Ping();
????????????System.Net.NetworkInformation.PingOptions?options?=?new?System.Net.NetworkInformation.PingOptions();
????????????options.DontFragment?=?true;
????????????string?data?=?"Test?Data!";
????????????byte[]?buffer?=?Encoding.ASCII.GetBytes(data);
????????????int?timeout?=?1000;?//?Timeout?時間,單位:毫秒
????????????System.Net.NetworkInformation.PingReply?reply?=?p.Send(ip,?timeout,?buffer,?options);
????????????if?(reply.Status?==?System.Net.NetworkInformation.IPStatus.Success)
????????????????return?true;
????????????else
????????????????return?false;
????????}
1. 根據IP地址獲得主機名稱
????????///?<summary>????????///?根據IP地址獲得主機名稱
????????///?</summary>
????????///?<param?name="ip">主機的IP地址</param>
????????///?<returns>主機名稱</returns>
????????public?string?GetHostNameByIp(string?ip)
????????{
????????????ip?=?ip.Trim();
????????????if?(ip?==?string.Empty)
????????????????return?string.Empty;
????????????try
????????????{
????????????????//?是否?Ping?的通
????????????????if?(this.Ping(ip))
????????????????{
????????????????????System.Net.IPHostEntry?host?=?System.Net.Dns.GetHostEntry(ip);
????????????????????return?host.HostName;
????????????????}
????????????????else
????????????????????return?string.Empty;
????????????}
????????????catch?(Exception)
????????????{
????????????????return?string.Empty;
????????????}
????????}
說明:如果你的電腦可以上網你甚至可以查詢到:IP地址“64.233.189.104”是 Google 的一個名為“hk-in-f104.google.com”的主機的IP地址。
關于代碼中 this.Ping(ip) 方法后面再說。
既然說了如何“根據IP地址獲得主機名稱”,那就要再說說如何“根據主機名獲得主機的IP地址”吧。
2. 根據主機名獲得主機的IP地址
????????///?<summary>
????????///?根據主機名(域名)獲得主機的IP地址
????????///?</summary>
????????///?<param?name="hostName">主機名或域名</param>
????????///?<example>GetIPByDomain("pc001");?GetIPByDomain("www.google.com");</example>
????????///?<returns>主機的IP地址</returns>
????????public?string?GetIpByHostName(string?hostName)
????????{
????????????hostName?=?hostName.Trim();
????????????if?(hostName?==?string.Empty)
????????????????return?string.Empty;
????????????try
????????????{
????????????????System.Net.IPHostEntry?host?=?System.Net.Dns.GetHostEntry(hostName);
????????????????return?host.AddressList.GetValue(0).ToString();
????????????}
????????????catch?(Exception)
????????????{
????????????????return?string.Empty;
????????????}
????????}
說明:如果你的電腦可以上網你甚至可以查詢到:“www.google.com”的IP地址是“64.233.189.104”。
最后,再說說C#實現簡單的 Ping 的功能,用于測試網絡是否已經聯通。
3. C#實現簡單的 Ping 的功能,用于測試網絡是否已經聯通
????????///?<summary>
????????///?是否能?Ping?通指定的主機
????????///?</summary>
????????///?<param?name="ip">ip?地址或主機名或域名</param>
????????///?<returns>true?通,false?不通</returns>
????????public?bool?Ping(string?ip)
????????{
????????????System.Net.NetworkInformation.Ping?p?=?new?System.Net.NetworkInformation.Ping();
????????????System.Net.NetworkInformation.PingOptions?options?=?new?System.Net.NetworkInformation.PingOptions();
????????????options.DontFragment?=?true;
????????????string?data?=?"Test?Data!";
????????????byte[]?buffer?=?Encoding.ASCII.GetBytes(data);
????????????int?timeout?=?1000;?//?Timeout?時間,單位:毫秒
????????????System.Net.NetworkInformation.PingReply?reply?=?p.Send(ip,?timeout,?buffer,?options);
????????????if?(reply.Status?==?System.Net.NetworkInformation.IPStatus.Success)
????????????????return?true;
????????????else
????????????????return?false;
????????}
轉載于:https://www.cnblogs.com/s021368/articles/1404677.html
總結
以上是生活随笔為你收集整理的根据IP地址获取主机名称的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫爬取ip记录网站信息并存
- 下一篇: Effective C#: Item 3