Unable to connect to a Telnet server
Posted by Mohammad Jawwad on 06 November 2008 02:42 AM
A correct sequence of steps must be followed to establish a telnet connection :|
1. Create a new Telnet instance providing the telnet server hostname or IP address as an argument to the constructor.
2. Subscribe delegate methods to newly created Telnet instance that will be responsible for capturing and responding to option negotiation events.
3. Subscribe delegate methods to newly created Telnet instance that will be responsible for capturing data received from telnet server.
4. Invoke the Telnet.Connect method to establish a connection.
Upon invoking the Telnet.Connect method a TCP connection will be established with the telnet server and option negotiation will be handled by the delegate methods subscribed in Step 2.
After option negotiation is completed any data received from the telnet server will be handled by the delegate method(s) subscribed in Step 3.
Why do I need to provide negotiation event handlers?
Upon establishing a connection with a telnet server the client and server may enter into option negotiation. Option negotiation is the process of both the client and server agreeing to a common set of features also known as options that will be supported by both the client and server terminals.
The problem is that you will not know which options are going to be offered by the server. If your code does not handle the option "offered" by the server the code will simply enter a endless wait cycle. Therefore to avoid this start with providing event handlers for all options. For example :
Telnet myTelnet = new Telnet(hostname);
myTelnet.DoOptionEvent += new Telnet.DoOptionEventHandler(OnDoOption);
public void OnDoOption(object sender, TelnetDoOptionEventArgs e)
Here we hook up the DoOptionEvent by providing a event handler for it. We use SendWontOption() method - reject the DoOption offered by the server. Do not assume that this event handler will always be invoked - it will only be invoked if the client/server enter option negotiation and the server offers the DO option.
Do the same for DontOptionEvent, WillOptionEvent and WontOptionEvent.
Next provide the event handlers for "data received" event :
myTelnet.DataReceivedEvent += new Telnet.DataReceivedEventHandler(OnDataReceived);
public void OnDataReceived(object sender, TelnetDataReceivedEventArgs e)
Now you should call the connect() method to establish the connection to the telnet server.