Telnet Primer

1/26/2009

History

In 1978, Digital Equipment Corporation (DEC) created the VT100. It was the first digital mass-market terminal to include many terminal graphic renditions. The terminal was on a choice of 80 or 132 column display. The VT100 family implemented the ANSI escape codes, which will be discussed later. Today the VT100 terminal has become the standard for terminal emulators, xterm being one of the more popular ones.

Connection

The server and client will send a series of DOs, DON'Ts, WILLs, and WON'Ts to each other. This is called negotiating and the start of each negotiation line is IAC (Interpret As Command, 0xFF). It is your job to correctly reply to its requests by telling what your client will do by sending your reply to negotiating commands along with your requests to the server.

Negotiation

For example if machine 1 (either the client or the server) sends a DO it must be replied by machine 2 with a WILL or a WON'T, depending on what the machine 2 chooses the implement -- so in general:

Syntax IAC NEGOTIATION(DO/DON'T/WILL/WON'T) OPTION (Most common ones are 0x00 to 0x31)

Definitions
IAC = 0xFF
DO = 0xFD
SUPPRESS_GO_AHEAD = 0x03

Example
IAC DO SUPPRESS_GO_AHEAD
0xFF 0xFD 0x03

Sub-Negotiation
Most negotiation options are on or off but some require more information.

Definitions
TERMINAL_TYPE = 0x18
SUB_NEGOTIATE = 0xFA
SUB_NEGOTIATE_END = 0xF0
IS = 0x00

Example
Conversation between the server asking the client's terminal type and the client responding with XTERM:

Server: IAC DO TERMINAL_TYPE
Client: IAC WILL TERMINAL_TYPE
Server: IAC SUB_NEGOTIATE TERMINAL_TYPE
Client: IAC SUB_NEGOTIATE TERMINAL_TYPE IS "XTERM" SUB_NEGOTIATE_END

After Negotiation

Once the server is finished with its primary negotiations, it will start sending data. A chunk of data begins with a byte other than IAC (unless followed by two 0xFF's). Furthermore, terminal controls implementing the ANSI escape control start with ESC[ (known as Control Sequence Introducer or CSI) followed by the control information.

Terminal Control

As mentioned above, within the data packet are terminal controls. They start with the ESC[ (0x1B 0x5B) bytes and (always?) end with the NULL byte (0x00). These terminal controls can range from status, setup, fonts, cursor, scrolling, tabs, erasing, printing, keyboard, and colors. Probably the most prominent of these are cursor and color control.

Color Control Syntax
ESC[{attr1};...;{attrn}m

Color Control Example
ESC[42mNULL ESC[34mGreen Background with Blue ForgroundNULL ESC[0mNULL
The first control sets the background to green, the next control sets the foreground to blue, outputs the text, then finally resets the terminal to default colors.

C# Code

Here are two C# classes I wrote in light of helping with myself with telnet programming. The code is still being developed and was written for my purposes so some things may not be what you need. However, you can download it and modify/add what you need:

Readings and Resources

Telnet programming with C# isn't all that bad -- with the correct resources.
Here are some I used:

Telnet

Terminal (xterm / VT100)