コンピュータが動作して応答しているかどうかを確認する最も簡単な方法は何ですか (ping/NetBios など)? 時間制限できる決定論的な方法が欲しいです。
1 つの解決策は、別のスレッドで共有 (File.GetDirectories(@"\compname")) に簡単にアクセスし、時間がかかりすぎる場合はスレッドを強制終了することです。
コンピュータが動作して応答しているかどうかを確認する最も簡単な方法は何ですか (ping/NetBios など)? 時間制限できる決定論的な方法が欲しいです。
1 つの解決策は、別のスレッドで共有 (File.GetDirectories(@"\compname")) に簡単にアクセスし、時間がかかりすぎる場合はスレッドを強制終了することです。
簡単!System.Net.NetworkInformation
名前空間の ping 機能を使用してください。
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を参照してください。