public void CheckOpenPortByBeginConnect() { var port = ProcessingPort++; try { var timeout = 5000; var newClient = new TcpClient(); var state = new IsTcpPortOpen { MainClient = newClient, tcpOpen = true }; IAsyncResult ar = newClient.BeginConnect("trungtvq.ddns.net", port, AsyncCallback, state); if (ar.AsyncWaitHandle.WaitOne(timeout, false) == false || newClient.Connected == false) { //throw new Exception(); return; } OpenedPortList.Add(port); //System.Diagnostics.Debug.WriteLine("Open at" + port); } catch { System.Diagnostics.Debug.WriteLine("Close at" + port); } }
public void CheckOpenPortByConnect() { var port = ProcessingPort++; while (port <= this.Stop) { using (TcpClient tcpClient = new TcpClient(AddressFamily.InterNetwork)) try { tcpClient.Connect(HostAddress, port); OpenedPortList.Add(port); System.Diagnostics.Debug.WriteLine("Open at" + port); } catch { //System.Diagnostics.Debug.WriteLine("Close at" + port); } finally { tcpClient.Close(); } port = ProcessingPort++; } }