.NET ez 2014-02-21
如果要監測某個 Port 是否正常,可以使用以下方式進行偵測。
static bool Check(string IPStr, int Port, int Timeout) { bool success = false; try { using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) success = socket.BeginConnect(IPAddress.Parse(IPStr), Port, null, null).AsyncWaitHandle.WaitOne(Timeout, true); } catch { } return success; }
標籤: .NET