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, 
telnet1.ClientOptions.Add(new Option(OptionCode.WindowSize, null,
        new Byte[] { ((Byte)(0)), ((Byte)(80)), ((Byte)(0)), ((Byte)(24)) }, 
telnet1.ClientOptions.Add(new Option(OptionCode.TerminalType,
        new Byte[] { ((Byte)(0)), ((Byte)(116)), ((Byte)(116)), ((Byte)(121)) }, 

telnet1.ServerOptions.Add(new Option(OptionCode.SuppressGoAheads, null,
telnet1.ServerOptions.Add(new Option(OptionCode.Echo, nullOptionState.RequestOn));
telnet1.ServerOptions.Add(new Option(OptionCode.OutputPageSize, null,


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.
        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.
    catch (Exception ex)
        //Close the connection if there is an error.

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.