PowerTCP Telnet for .NET

from $349.00
Available Platforms

See all PowerTCP Telnet for .NET Code Examples

 Open Negotiation Example

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

 

//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));

 

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.

 

using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

Telnet telnet1 = new Telnet();

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();
    }
}

private bool remoteCertificateValidation(
   object sender,
   X509Certificate remoteCertificate,
   X509Chain chain,
   SslPolicyErrors sslPolicyErrors)
{
    return true; //Accepts any certificate, even if invalid
}

 

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