public static IConnection Build(string hostName, int port, int timeout = 0) { try { var client = new TcpClient(); client.Connect(hostName, port); var connection = new TcpClientConnection(client, timeout); return connection; } catch (SocketException ex) { throw new ConnectionException(string.Format("Unable to create a connection to the host '{0}' on the port '{1}'.", hostName, port), ex); } }
// ----- Internal logics private void Callback(IAsyncResult result) { try { var listener = (TcpListener)result.AsyncState; var client = listener.EndAcceptTcpClient(result); var connection = new TcpClientConnection(client); connection.Disconnected += ConnectionOnDisconnected; OnConnectionEstablished(new ConnectionEstablishedEventArgs(connection)); listener.BeginAcceptTcpClient(Callback, listener); } catch (ObjectDisposedException) {} }
// ----- Internal logics private void Callback(IAsyncResult result) { try { var listener = (TcpListener)result.AsyncState; var client = listener.EndAcceptTcpClient(result); var connection = new TcpClientConnection(client); connection.Disconnected += ConnectionOnDisconnected; OnConnectionEstablished(new ConnectionEstablishedEventArgs(connection)); listener.BeginAcceptTcpClient(Callback, listener); } catch (ObjectDisposedException) {} }
public static IConnection Build(string hostName, int port, int timeout = 0) { try { var client = new TcpClient(); client.Connect(hostName, port); var connection = new TcpClientConnection(client, timeout); return(connection); } catch (SocketException ex) { throw new ConnectionException(string.Format("Unable to create a connection to the host '{0}' on the port '{1}'.", hostName, port), ex); } }