PowerTCP Telnet for .NET | Windows Forms Option Negotiation Code Examples

See all PowerTCP Telnet for .NET Samples and Code Examples

 

The following examples demonstrates manual client/server option negotiation. Typically, the Telnet component will handle option negotiation manually. However, the developer also has the flexibility to manually set options to be used and rejected.

 

if (telnet1.RemoteEndPoint.Port == 23)
{
    //Standard telnet port, add standard telnet options.
    telnet1.ClientOptions.Add(new Option(OptionCode.SuppressGoAheads, 
        OptionState.RequestOn));
    telnet1.ClientOptions.Add(new Option(OptionCode.WindowSize, null,
        new Byte[] { ((Byte)(0)), ((Byte)(80)), ((Byte)(0)), ((Byte)(24)) }, 
        OptionState.RequestOn));
    telnet1.ClientOptions.Add(new Option(OptionCode.TerminalType,
        new Byte[] { ((Byte)(0)), ((Byte)(116)), ((Byte)(116)), ((Byte)(121)) }, 
        OptionState.RequestOn));

    telnet1.ServerOptions.Add(new Option(OptionCode.SuppressGoAheads, null,
        OptionState.RequestOn));
    telnet1.ServerOptions.Add(new Option(OptionCode.Echo, nullOptionState.RequestOn));
    telnet1.ServerOptions.Add(new Option(OptionCode.OutputPageSize, null,
        OptionState.RequestOn));
}
else
{
    //Non-standard telnet port, do not send telnet options.
    telnet1.ClientOptions.Clear();
    telnet1.ServerOptions.Clear();
}


 

In addition, negotiation can be handled in real-time, using the CommandReceived event and SendXXX commands. In the following example, explicit security is negotiated in the CommandReceived event handler.

 

if (telnet1.RemoteEndPoint.Port == 23)
{
    //Standard telnet port, add standard telnet options.
    telnet1.ClientOptions.Add(new Option(OptionCode.SuppressGoAheads, 
        OptionState.RequestOn));
    telnet1.ClientOptions.Add(new Option(OptionCode.WindowSize, null,
        new Byte[] { ((Byte)(0)), ((Byte)(80)), ((Byte)(0)), ((Byte)(24)) }, 
        OptionState.RequestOn));
    telnet1.ClientOptions.Add(new Option(OptionCode.TerminalType,
        new Byte[] { ((Byte)(0)), ((Byte)(116)), ((Byte)(116)), ((Byte)(121)) }, 
        OptionState.RequestOn));

    telnet1.ServerOptions.Add(new Option(OptionCode.SuppressGoAheads, null,
        OptionState.RequestOn));
    telnet1.ServerOptions.Add(new Option(OptionCode.Echo, nullOptionState.RequestOn));
    telnet1.ServerOptions.Add(new Option(OptionCode.OutputPageSize, null,
        OptionState.RequestOn));
}
else
{
    //Non-standard telnet port, do not send telnet options.
    telnet1.ClientOptions.Clear();
    telnet1.ServerOptions.Clear();
}

 

In addition, negotiation can be handled in real-time, using the CommandReceived event and SendXXX commands. In the following example, explicit security is negotiated in the CommandReceived event handler.

 

private void telnet1_CommandReceived(object sender, CommandEventArgs e)
{
    //Handles negotiation for an explicitly secure connection.
    try
    {
        if (e.OptionCode == OptionCode.Authentication)
        {
            if (e.Command == Command.Do)
                telnet1.SendOption(Command.Will, OptionCode.Authentication);
            else if (e.Command == Command.SB && e.SubOption[0] == 1)
            {
                //Server expects a SEND request (IS).
                byte[] response = new byte[4];
                response[0] = 0; // is
                response[1] = 7; // ssl
                response[2] = 0; // AuthClientToServer
                response[3] = 1; // START_SSL is our request
                telnet1.SendSubOption(e.OptionCode, response);
            }
            else if (e.Command == Command.SB && e.SubOption[0] == 2)
            {
                //Setup client security with the target host, and a callback that
                //executes when the remote host presents its certificate.
                ClientSecurity security = new ClientSecurity();
                security.TargetHost = "mySecureHost";
                security.ValidationCallback += remoteCertificateValidation;

                //Authenticate the server.
                telnet1.AuthenticateAsClient(security);
            }
        }
    }
    catch (Exception ex)
    {

        //Close the connection if there is an error.
        telnet1.Close();
        telnet1.Marshal(ex);
    }
}

 

To download a trial please visit the PowerTCP Telnet for .NET product page.