c#如何检查局域网内服务器是否连通
2006-01-21 21:10
309 查看
方法一用ping:
/// <summary>
/// pingcmd 的摘要说明。
/// </summary>
public delegate void pingcmd_completed(object sender,PingEventArgs e);
public class pingcmd
{
private Process myp;
public event pingcmd_completed pingcom;//发布事件
private string [] s;
public pingcmd(string [] a) //构造函数
{
//
// TODO: 在此处添加构造函数逻辑
//
this.s=a;
myp=new Process();
myp.StartInfo.UseShellExecute=false;
myp.StartInfo.RedirectStandardError=true;
myp.StartInfo.RedirectStandardInput=true;
myp.StartInfo.RedirectStandardOutput=true;
myp.StartInfo.CreateNoWindow=true;
myp.StartInfo.FileName="cmd.exe";
}
public void runping()//线程调用方法
{
ping(this.s);
}
// public void ping(string ip)//执行ping方法
// {
// myp.Start();
// myp.StandardInput.WriteLine("ping -n 1 "+ip);
// myp.StandardInput.WriteLine("exit");
// string strRst=myp.StandardOutput.ReadToEnd();
// string pingrst="";
// myp.Close();
// if(strRst.IndexOf("(0% loss)")!=-1)
// pingrst = " 连 接 正 常 ";
// else if( strRst.IndexOf("Destination host unreachable")!=-1)
// pingrst = "无法到达目的主机";
// else if(strRst.IndexOf("Request timed out")!=-1)
// pingrst = " 连 接 超 时 ";
// else if(strRst.IndexOf("Unknown host")!=-1)
// pingrst = " 无法解析主机 ";
//
// onpingcomplete(this,new PingEventArgs(new string[,] {{ip,pingrst}}));
//
// }
public int ping(string ip)//执行ping方法
{
myp.Start();
myp.StandardInput.WriteLine("ping -n 1 "+ip);
myp.StandardInput.WriteLine("exit");
string strRst=myp.StandardOutput.ReadToEnd();
int pingrst = 4;
myp.Close();
if(strRst.IndexOf("(0% loss)")!=-1)
pingrst = 0;// " 连 接 正 常 ";
else if( strRst.IndexOf("Destination host unreachable")!=-1)
pingrst = 1;//"无法到达目的主机";
else if(strRst.IndexOf("Request timed out")!=-1)
pingrst = 2;//" 连 接 超 时 ";
else if(strRst.IndexOf("Unknown host")!=-1)
pingrst = 3;//" 无法解析主机 ";
return pingrst;
}
public void ping(string [] ips)
{
foreach(string ip in ips)
ping(ip);
}
protected virtual void onpingcomplete(object sender,PingEventArgs e)//虚方法
{
pingcom(sender,e);
}
}//pingcmd类
////////////////////////
//参数类
/// //////////////
public class PingEventArgs :EventArgs
{
private string [,] pingresult;
public PingEventArgs(string [,] s)
{
this.pingresult=s;
}
//ping结果
public string [,] ping_result
{
get
{
return this.pingresult;
}
}
}
方法二,用Socket
/// <summary>
/// 是否可以连接到某一个站点
/// </summary>
/// <param name="server">主机名</param>
/// <param name="port">端口</param>
/// <returns>true 可以连接 ;false 连接不上</returns>
public Socket CanConnect(string server,int port)
{
Socket RtnSocket = null;
IPHostEntry hostEntry = null;
if(server == string.Empty)
throw new Exception("主机名不能为空");
// Get host related information.
try
{
hostEntry = Dns.Resolve(server);
}
catch(ArgumentNullException)
{
throw new Exception("主机名为空引用");
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
catch(SecurityException)
{
throw new Exception("调用方没有访问 DNS 信息的权限");
}
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
RtnSocket = tempSocket;
break;
}
else
{
continue;
}
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
}
return RtnSocket;
}
/// <summary>
/// 是否可以连接到某一个站点
/// </summary>
/// <param name="server">主机名</param>
/// <param name="port">端口</param>
/// <returns>true 可以连接 ;false 连接不上</returns>
public Socket CanConnect(string server,int port)
{
Socket RtnSocket = null;
IPHostEntry hostEntry = null;
if(server == string.Empty)
throw new Exception("主机名不能为空");
// Get host related information.
try
{
hostEntry = Dns.Resolve(server);
}
catch(ArgumentNullException)
{
throw new Exception("主机名为空引用");
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
catch(SecurityException)
{
throw new Exception("调用方没有访问 DNS 信息的权限");
}
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
RtnSocket = tempSocket;
break;
}
else
{
continue;
}
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
}
return RtnSocket;
}
/// <summary>
/// pingcmd 的摘要说明。
/// </summary>
public delegate void pingcmd_completed(object sender,PingEventArgs e);
public class pingcmd
{
private Process myp;
public event pingcmd_completed pingcom;//发布事件
private string [] s;
public pingcmd(string [] a) //构造函数
{
//
// TODO: 在此处添加构造函数逻辑
//
this.s=a;
myp=new Process();
myp.StartInfo.UseShellExecute=false;
myp.StartInfo.RedirectStandardError=true;
myp.StartInfo.RedirectStandardInput=true;
myp.StartInfo.RedirectStandardOutput=true;
myp.StartInfo.CreateNoWindow=true;
myp.StartInfo.FileName="cmd.exe";
}
public void runping()//线程调用方法
{
ping(this.s);
}
// public void ping(string ip)//执行ping方法
// {
// myp.Start();
// myp.StandardInput.WriteLine("ping -n 1 "+ip);
// myp.StandardInput.WriteLine("exit");
// string strRst=myp.StandardOutput.ReadToEnd();
// string pingrst="";
// myp.Close();
// if(strRst.IndexOf("(0% loss)")!=-1)
// pingrst = " 连 接 正 常 ";
// else if( strRst.IndexOf("Destination host unreachable")!=-1)
// pingrst = "无法到达目的主机";
// else if(strRst.IndexOf("Request timed out")!=-1)
// pingrst = " 连 接 超 时 ";
// else if(strRst.IndexOf("Unknown host")!=-1)
// pingrst = " 无法解析主机 ";
//
// onpingcomplete(this,new PingEventArgs(new string[,] {{ip,pingrst}}));
//
// }
public int ping(string ip)//执行ping方法
{
myp.Start();
myp.StandardInput.WriteLine("ping -n 1 "+ip);
myp.StandardInput.WriteLine("exit");
string strRst=myp.StandardOutput.ReadToEnd();
int pingrst = 4;
myp.Close();
if(strRst.IndexOf("(0% loss)")!=-1)
pingrst = 0;// " 连 接 正 常 ";
else if( strRst.IndexOf("Destination host unreachable")!=-1)
pingrst = 1;//"无法到达目的主机";
else if(strRst.IndexOf("Request timed out")!=-1)
pingrst = 2;//" 连 接 超 时 ";
else if(strRst.IndexOf("Unknown host")!=-1)
pingrst = 3;//" 无法解析主机 ";
return pingrst;
}
public void ping(string [] ips)
{
foreach(string ip in ips)
ping(ip);
}
protected virtual void onpingcomplete(object sender,PingEventArgs e)//虚方法
{
pingcom(sender,e);
}
}//pingcmd类
////////////////////////
//参数类
/// //////////////
public class PingEventArgs :EventArgs
{
private string [,] pingresult;
public PingEventArgs(string [,] s)
{
this.pingresult=s;
}
//ping结果
public string [,] ping_result
{
get
{
return this.pingresult;
}
}
}
方法二,用Socket
/// <summary>
/// 是否可以连接到某一个站点
/// </summary>
/// <param name="server">主机名</param>
/// <param name="port">端口</param>
/// <returns>true 可以连接 ;false 连接不上</returns>
public Socket CanConnect(string server,int port)
{
Socket RtnSocket = null;
IPHostEntry hostEntry = null;
if(server == string.Empty)
throw new Exception("主机名不能为空");
// Get host related information.
try
{
hostEntry = Dns.Resolve(server);
}
catch(ArgumentNullException)
{
throw new Exception("主机名为空引用");
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
catch(SecurityException)
{
throw new Exception("调用方没有访问 DNS 信息的权限");
}
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
RtnSocket = tempSocket;
break;
}
else
{
continue;
}
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
}
return RtnSocket;
}
/// <summary>
/// 是否可以连接到某一个站点
/// </summary>
/// <param name="server">主机名</param>
/// <param name="port">端口</param>
/// <returns>true 可以连接 ;false 连接不上</returns>
public Socket CanConnect(string server,int port)
{
Socket RtnSocket = null;
IPHostEntry hostEntry = null;
if(server == string.Empty)
throw new Exception("主机名不能为空");
// Get host related information.
try
{
hostEntry = Dns.Resolve(server);
}
catch(ArgumentNullException)
{
throw new Exception("主机名为空引用");
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
catch(SecurityException)
{
throw new Exception("调用方没有访问 DNS 信息的权限");
}
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
tempSocket.Connect(ipe);
if(tempSocket.Connected)
{
RtnSocket = tempSocket;
break;
}
else
{
continue;
}
}
catch(SocketException sx)
{
throw new Exception(sx.Message);
}
}
return RtnSocket;
}
相关文章推荐
- c#如何检查局域网内服务器是否连通
- c#如何检查局域网内服务器是否连通(调用外部Ping命令获取网络连接情况)
- c#如何检查局域网内服务器是否连通(调用外部Ping命令获取网络连接情况)
- c#如何检查局域网内服务器是否连通(调用外部Ping命令获取网络连接情况)
- 如何检查linux服务器的端口是否被防火墙挡住
- 服务器被CC***的症状以及如何检查是否被CC***
- 如何检查 Windows 2008服务器版本,是否打过SP1
- 如何检查服务器是否被google处罚过
- Java如何检查List<String> 里是否有想要的字符串?
- linux如何判断服务器是否被入侵
- 如何验证远程服务器上文件是否存在
- 如何访问局域网中的DB服务器
- C# 检查网络是否连通 判断远程文件是否存在 C#获取程序路径的方法中需要注意的地方
- 检查文件是否存在于远程服务器上
- 如何自我检查网站的优化是否做得到位
- 如何检查字符是否是整数并输出其整数值
- 检查服务器是否开启GD库
- 检查本地服务器是否配置成功
- 如何检查系统是否支持Zend Optimizer
- 检查apache服务器是否正在运行的脚本