public static ScanMessage Connect(EndPoint remoteEndPoint, int timeout) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); TcpConnectCall connectCall = new TcpConnectCall(socket); // Need for controlling timeout period introduces us to asynccallback methods AsyncCallback connectedCallback = new AsyncCallback(connectCall.ConnectedCallback); try { IAsyncResult result = socket.BeginConnect(remoteEndPoint, connectedCallback, socket); // wait for timeout to connect if (result.AsyncWaitHandle.WaitOne(timeout, false) == false) { return ScanMessage.Timeout; } else { Exception connectException = connectCall.connectFailureException; if (connectException != null) { return ScanMessage.PortClosed; } return ScanMessage.PortOpened; } } finally { socket.Close(); } }
public static ScanMessage Connect(EndPoint remoteEndPoint, int timeout) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); TcpConnectCall connectCall = new TcpConnectCall(socket); // Need for controlling timeout period introduces us to asynccallback methods AsyncCallback connectedCallback = new AsyncCallback(connectCall.ConnectedCallback); try { IAsyncResult result = socket.BeginConnect(remoteEndPoint, connectedCallback, socket); // wait for timeout to connect if (result.AsyncWaitHandle.WaitOne(timeout, false) == false) { return(ScanMessage.Timeout); } else { Exception connectException = connectCall.connectFailureException; if (connectException != null) { return(ScanMessage.PortClosed); } return(ScanMessage.PortOpened); } } finally { socket.Close(); } }