局域网中根据IP地址反查主机的名称(C#)
来源:互联网 发布:中科大软件学院多少钱 编辑:程序博客网 时间:2024/05/02 02:54
你遇到过这种情况吗?你的防火墙报告局域网中的某个IP地址的电脑正在攻击你,但是防火墙却没有提示发出攻击的电脑名称,到底谁的电脑在攻击呢(攻击你的电脑可能是中毒了)?有一天早上你刚刚上班,打开电脑后发现连接不了服务器,到服务器那里一看才知道,原来有人使用了服务器的IP地址,到底谁在使用服务器的IP地址呢?nslookup 可以实现域名(主机名)的反查IP地址。哈哈,但今天说的是用C#实现。
1. 根据IP地址获得主机名称
说明:如果你的电脑可以上网你甚至可以查询到:IP地址“64.233.189.104”是 Google 的一个名为“hk-in-f104.google.com”的主机的IP地址。
关于代码中 this.Ping(ip) 方法后面再说。
既然说了如何“根据IP地址获得主机名称”,那就要再说说如何“根据主机名获得主机的IP地址”吧。
2. 根据主机名获得主机的IP地址
说明:如果你的电脑可以上网你甚至可以查询到:“www.google.com”的IP地址是“64.233.189.104”。
最后,再说说C#实现简单的 Ping 的功能,用于测试网络是否已经联通。
3. C#实现简单的 Ping 的功能,用于测试网络是否已经联通
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地址获得主机名称
/// </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;
}
}
/// 根据主机名(域名)获得主机的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;
}
/// 是否能 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;
}
- 局域网中根据IP地址反查主机的名称(C#)
- 局域网中根据IP地址反查主机的名称(C#)
- 根据ip地址获取主机名称
- C#实现局域网IP的扫描,从而打印出IP地址,主机名称和MAC地址
- MFC如何获取同一局域网中主机的IP地址
- 获取局域网所有IP地址和主机名称
- 局域网怎么通过IP查看对方MAC,用IP反查局域网MAC地址
- Linux如何根据IP地址反查hostname
- 已知IP查主机名称
- 根据已知IP地址查询局域网中对应网卡的MAC地址
- 获取局域网中所有主机与IP地址
- 根据IP反查用户
- DNS服务中取得主机名称和IP地址
- 获取局域网内的smsblserver主机的IP地址代码
- linux下查看局域网的所有主机的ip地址
- 在局域网中根据IP地址获得计算机名
- 实现获取主机名称,IP地址的类 (java)
- 局域网通过IP反查主机名
- jdk在solaris上的安装方法
- 根据文件名或文件扩展名获取文件的默认图标
- 唤醒自我身上的精灵
- SQL Server 2000/2005 分页SQL — 单条SQL语句
- 编写高效的JAVA程序-编程规范 (基本篇 )
- 局域网中根据IP地址反查主机的名称(C#)
- C++学习笔记(一)
- C# 启动外部程序的几种方法
- _IO,_IOR,_IOW和_IORW的含义
- Patrick Henry Winston 个人主页内容翻译
- 机器启动时, 要按F1的问题
- Delphi与Excel的亲密接触
- MagicAjax的使用方法
- DataGridView 密码列(显示为*号)的设置