c#如何检查局域网内服务器是否连通

方法一用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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值