/// <summary> /// This constructor simplifies the process of correctly creating a socket, /// connecting and creating an instance of TCPConnection, where the stream /// and other I/O properties are exposed and managed. The constructor simplifies /// descendents by accepting only one parameter where both the host and port can /// be specified, allowing the developer to avoid constructor hell. /// </summary> /// <param name="aHost"> /// This string parameter can contain both the host and port, or just /// the host name. When including the port with the host use a ":" to seperate /// the two, eg "localhost:80". /// </param> public TCPClient(string aHost) { Socket xSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); int xPort; string[] xHostParts = aHost.Split(':'); if (xHostParts.Length == 2) { xPort = int.Parse(xHostParts[1]); } else { xPort = GetDefaultPort(); if (xPort == -1) { throw new Exceptions.Exception("No default port specified."); } } xSocket.Connect(new IPEndPoint(ResolveAddress(xHostParts[0]), xPort)); mConnection = new TCPConnection(xSocket); }
/// <summary> /// This constructor is here to give the developer more control in the creation /// of there TCPConnection. Once having created the TCPConnection it is passed /// as a parameter into the consctuctior. /// </summary> /// <param name="aConnection">TCPConnection for the TCPClient</param> public TCPClient(TCPConnection aConnection) { mConnection = aConnection; }
protected void ConnectionThreadRun(object aSocket) { var xConnection = new TCPConnection((Socket)aSocket); mDoRun(xConnection); }