TCP ポートが空いているかどうかをテストする必要があります。私はこの方法を書きました:
private bool freePort(int port) {
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = socket.BeginConnect(IPAddress.Loopback, port, null, null);
bool success = result.AsyncWaitHandle.WaitOne(1000, true);
try{
socket.Close();
}catch(Exception){}
return !success;
}
これは Windows 7 では機能しますが、Windows XP では機能しません。
winXPでは時々動作し、時々間違った答えを出します...