示例#1
0
    /// <summary>
    /// Connects to the remote server and starts receiving data to write to the display.
    /// </summary>
    public void Connect(object state)
    {
        //Necessary for explicit security
        EventHandler <CommandEventArgs> commandReceivedHandler =
            new EventHandler <CommandEventArgs>(telnet_CommandReceived);

        try
        {
            //Connect to the server
            Telnet.Connect(session);
            Telnet.Socket.ReceiveTimeout = 5000;

            //If Explicit security used, use the CommandReceived event to negotiate
            if (securityType == SecurityType.Explicit)
            {
                Telnet.CommandReceived += commandReceivedHandler;
            }
            //If Implicit security used, authenticate server and specify certificate callback functions
            else if (securityType == SecurityType.Implicit)
            {
                Telnet.AuthenticateAsClient(security);
            }

            //Login and provide transferred data to the interface (Data event)
            if (session.RemoteEndPoint.Port > 0 && credentials.Username.Length > 0 &&
                credentials.Password.Length > 0)
            {
                Data data = Telnet.Login(credentials);
                Telnet.Marshal(data, string.Empty, null);
            }

            //Provide telnet.Stream to interface (UserState event), or start the read loop.
            Telnet.Socket.ReceiveTimeout = 0;
            if (ReceiveLoopRequired)
            {
                ReceiveData();
            }
            else
            {
                Telnet.Marshal("", Telnet.GetStream());
            }
        }
        catch (Exception ex)
        {
            Telnet.Marshal(ex);
        }
        finally
        {
            Telnet.CommandReceived -= commandReceivedHandler;
        }
    }