Beispiel #1
0
        /// <summary>
        /// Closes external resources.
        /// Safe, can be called multiple times
        /// </summary>
        public void Close()
        {
            // physical connection
            if (this._tcpClient != null)
            {
                try
                {
                    // it is important to close the stream
                    // because somehow tcpClient does not physically breaks down
                    // the connection - on "one connection" telnet server the 
                    // server remains blocked if not doing it!
                    this._tcpClient.GetStream().Close();
                    this._tcpClient.Close();
                    this._tcpClient = null;
                }
                catch
                {
                    this._tcpClient = null;
                }
            }

            // clean up
            // fast, "can be done several" times
            this._virtualScreen = null;
            this._buffer = null;
            this._callBackReceive = null;
            this._callBackSend = null;
            this._forceLogout = false;
        }
Beispiel #2
0
        /// <summary>
        /// Connect to the telnet server
        /// </summary>
        /// <returns>true if connection was successful</returns>
        public bool Connect()
        {
            // check for buffer
            if (this._buffer == null)
                this._buffer = new byte[ReceiveBufferSize];

            // virtual screen
            if (this._virtualScreen == null)
                this._virtualScreen = new VirtualScreen(this._vsWidth, this._vsHeight, 1, 1);

            // set the callbacks
            if (this._callBackReceive == null)
                this._callBackReceive = this.ReadFromStream;
            if (this._callBackSend == null)
                this._callBackSend = this.WriteToStream;

            // flags
            this._serverEcho = false;
            this._clientInitNaws = false;
            this._firstResponse = true;
            this._nawsNegotiated = false;
            this._forceLogout = false;

            // return physical connection
            if (this._tcpClient != null)
                return true; // we still have a connection -> ?? better reconnect ??
            try
            {
                // TODO: Improve performance ...?
                // This is not working:	IPAddress ipAddress = IPAddress.Parse(this.hostName);
                //						IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port);
                // because it addresses local endpoints
                this._tcpClient = new TcpClient(this._hostName, this._port) {ReceiveTimeout = this._timeoutReceive, SendTimeout = this._timeoutSend, NoDelay = true};
                this._tcpClient.GetStream().BeginRead(this._buffer, 0, this._buffer.Length, this._callBackReceive, null);
                return true;
            }
            catch
            {
                this._tcpClient = null;
                return false;
            }
        }