用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通
2015-12-17 10:34
996 查看
用C#实现实现简单的
Ping 的功能,用于测试网络是否已经联通
1.
根据IP地址获得主机名称
[c-sharp]
view plaincopyprint?
/// <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;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
说明:如果你的电脑可以上网你甚至可以查询到:IP地址“64.233.189.104”是
Google 的一个名为“hk-in-f104.google.com”的主机的IP地址。
关于代码中 this.Ping(ip)
方法后面再说。
既然说了如何“根据IP地址获得主机名称”,那就要再说说如何“根据主机名获得主机的IP地址”吧。
2.
根据主机名获得主机的IP地址
[c-sharp]
view plaincopyprint?
/// <summary>
/// 根据主机名(域名)获得主机的IP地址
/// </summary>
/// <param name="hostName">主机名或域名</param>
/// <example> 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;
}
}
文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
说明:如果你的电脑可以上网你甚至可以查询到:“www.google.com”的IP地址是“64.233.189.104”。
最后,再说说C#实现简单的
Ping 的功能,用于测试网络是否已经联通。
3. C#实现简单的
Ping 的功能,用于测试网络是否已经联通
[c-sharp]
view plaincopyprint?
/// <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;
}
private void button1_Click(object sender, EventArgs e)//获取IP
{ textBox2.Text=GetIpByHostName(textBox1.Text); }
private void button2_Click(object sender, EventArgs e)//获取主机名
{ textBox1.Text=GetHostNameByIp(textBox2.Text); }
private void button3_Click(object sender, EventArgs e)//获取本机IP
{
string hostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);
//Dns.GetHostByName(myName);已过时
IPAddress[] addr = ipEntry.AddressList;
textBox2.Text = addr[0].ToString();
}
/// <summary>
/// 局域网搜索事件
/// </summary>
private void LanSearch()
{
string localhost = (Dns.GetHostEntry(Dns.GetHostName()))
.AddressList[0].ToString(); //本地主机IP地址
string str = localhost.Substring(0, localhost.LastIndexOf("."));
for (int i = 0; i < 255; i++) //建立255个线程扫描IP
{
string ip = str + "." + i.ToString();
this.Text = ip;
if (Ping(ip))
{
listBox1.Items.Add(ip);
Application.DoEvents();
}
progressBarSearch.Value = progressBarSearch.Value + 1;
}
}
private void button4_Click(object sender, EventArgs e)//在线主机
{
LanSearch();
}
用C#实现实现简单的
Ping 的功能,用于测试网络是否已经联通
1.
根据IP地址获得主机名称
[c-sharp]
view plaincopyprint?
/// <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;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
/// <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; } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
说明:如果你的电脑可以上网你甚至可以查询到:IP地址“64.233.189.104”是
Google 的一个名为“hk-in-f104.google.com”的主机的IP地址。
关于代码中 this.Ping(ip)
方法后面再说。
既然说了如何“根据IP地址获得主机名称”,那就要再说说如何“根据主机名获得主机的IP地址”吧。
2.
根据主机名获得主机的IP地址
[c-sharp]
view plaincopyprint?
/// <summary>
/// 根据主机名(域名)获得主机的IP地址
/// </summary>
/// <param name="hostName">主机名或域名</param>
/// <example> 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;
}
}
文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
/// <summary> /// 根据主机名(域名)获得主机的IP地址 /// </summary> /// <param name="hostName">主机名或域名</param> /// <example> 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; } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
说明:如果你的电脑可以上网你甚至可以查询到:“www.google.com”的IP地址是“64.233.189.104”。
最后,再说说C#实现简单的
Ping 的功能,用于测试网络是否已经联通。
3. C#实现简单的
Ping 的功能,用于测试网络是否已经联通
[c-sharp]
view plaincopyprint?
/// <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;
}
private void button1_Click(object sender, EventArgs e)//获取IP
{ textBox2.Text=GetIpByHostName(textBox1.Text); }
private void button2_Click(object sender, EventArgs e)//获取主机名
{ textBox1.Text=GetHostNameByIp(textBox2.Text); }
private void button3_Click(object sender, EventArgs e)//获取本机IP
{
string hostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);
//Dns.GetHostByName(myName);已过时
IPAddress[] addr = ipEntry.AddressList;
textBox2.Text = addr[0].ToString();
}
/// <summary>
/// 局域网搜索事件
/// </summary>
private void LanSearch()
{
string localhost = (Dns.GetHostEntry(Dns.GetHostName()))
.AddressList[0].ToString(); //本地主机IP地址
string str = localhost.Substring(0, localhost.LastIndexOf("."));
for (int i = 0; i < 255; i++) //建立255个线程扫描IP
{
string ip = str + "." + i.ToString();
this.Text = ip;
if (Ping(ip))
{
listBox1.Items.Add(ip);
Application.DoEvents();
}
progressBarSearch.Value = progressBarSearch.Value + 1;
}
}
private void button4_Click(object sender, EventArgs e)//在线主机
{
LanSearch();
}
/// <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(); a322 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; } private void button1_Click(object sender, EventArgs e)//获取IP { textBox2.Text=GetIpByHostName(textBox1.Text); } private void button2_Click(object sender, EventArgs e)//获取主机名 { textBox1.Text=GetHostNameByIp(textBox2.Text); } private void button3_Click(object sender, EventArgs e)//获取本机IP { string hostName = System.Net.Dns.GetHostName(); IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName); //Dns.GetHostByName(myName);已过时 IPAddress[] addr = ipEntry.AddressList; textBox2.Text = addr[0].ToString(); } /// <summary> /// 局域网搜索事件 /// </summary> private void LanSearch() { string localhost = (Dns.GetHostEntry(Dns.GetHostName())) .AddressList[0].ToString(); //本地主机IP地址 string str = localhost.Substring(0, localhost.LastIndexOf(".")); for (int i = 0; i < 255; i++) //建立255个线程扫描IP { string ip = str + "." + i.ToString(); this.Text = ip; if (Ping(ip)) { listBox1.Items.Add(ip); Application.DoEvents(); } progressBarSearch.Value = progressBarSearch.Value + 1; } } private void button4_Click(object sender, EventArgs e)//在线主机 { LanSearch(); }
相关文章推荐
- http以post方式上传一个文件,构造其请求头和消息报文 application/x-www-form-urlencoded multipart/form-data
- Centos下网络环境配置
- 网络工具
- android 特殊网络类型说明
- Jest is a Java HTTP Rest client for Elasticsearch
- win10+ubuntu14.04双系统有线网络问题
- Angular - - $http请求服务
- iOS网络编程(六)NSURLSession详解
- Exadata 的网络概述
- 网络PDF---BCM5325E与BCM5241
- JMeter学习(二十五)HTTP属性管理器
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- KVM的部署与优化(1)
- 网络协议分析
- 2016淘宝推广店铺思路太原网络营销师郭文军讲
- 利用一容器一IP机制在 Mesos中提高网络隔离效果
- Laurastar 中国网上商城正式上线 : http://shop.laurastar.cn
- SpringMVC ------HiddenHttpMethodFilter 过滤器
- python网络爬虫的记录
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包