题
什么是检查计算机是否活着和响应(说在平/的NetBios)最简单的方法? 我想,我可以时限确定性方法。
如果时间过长一种解决方案是简单访问的共享(File.GetDirectories(@“\ COMPNAME”))在一个单独的线程,并杀死该线程。
解决方案
轻松!使用System.Net.NetworkInformation
命名空间的平工厂!
http://msdn.microsoft.com /en-us/library/system.net.networkinformation.ping.aspx
其他提示
要已知的服务器上检查一个特定的TCP端口(myPort
),使用下面的代码段。您可以赶上System.Net.Sockets.SocketException
异常,表明非可用的端口。
using System.Net;
using System.Net.Sockets;
...
IPHostEntry myHostEntry = Dns.GetHostByName("myserver");
IPEndPoint host = new IPEndPoint(myHostEntry.AddressList[0], myPort);
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
s.Connect(host);
此外,专业化,检查可以尝试IO与插槽上的超时。
只要你想将自己的子网,你可以检查它使用 ARP 一中检查计算机
>。下面是一个例子:
//for sending an arp request (see pinvoke.net)
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(
int DestIP,
int SrcIP,
byte[] pMacAddr,
ref uint PhyAddrLen);
public bool IsComputerAlive(IPAddress host)
{
//can't check the own machine (assume it's alive)
if (host.Equals(IPAddress.Loopback))
return true;
//Prepare the magic
//this is only needed to pass a valid parameter
byte[] macAddr = new byte[6];
uint macAddrLen = (uint)macAddr.Length;
//Let's check if it is alive by sending an arp request
if (SendARP((int)host.Address, 0, macAddr, ref macAddrLen) == 0)
return true; //Igor it's alive!
return false;//Not alive
}
请参阅 Pinvoke.net 获得更多的信息。
不隶属于 StackOverflow