您的位置:首页 > 编程语言 > C#

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: