Take the 2-minute tour ×

I understand that Telnet is a protocol as much as HTTP. I have this notion that after the initial TCP connection is made the Telnet client would send some telnet specific code over to the server on the other side in this case a HTTP server. But since HTTP server doesn't understand Telnet specific codes it should throw an error or drop the connection etc. But in reality we can telnet to a HTTP server and fetch pages if we type in correct HTTP headers and send them. How can it be like that? Wikipedia entry really didn't help me to understand this specific point. (http://en.wikipedia.org/wiki/Telnet#Telnet_data)

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

You are right in saying that telnet is its own protocol, which is described in a couple of RFC's. You can take a look at wikipedia to see which ones exactly and some other resources that explain the protocol.

Basically it works like this: when you use telnet to connect to a server, it will show every printable character the server sends to you. And everything you type will be sent back to the server. This is how you can retrieve web pages when connecting to a web server: you send a command that a http server accepts and get the correct result.

Now, there are a few telnet specifics option. IIRC, you won't send them to the server unless the server sends them first. Those options are used to enable/disable local echo (think about passwords, you don't want those visible when logging in), negotiate terminal size, negotiate end of line type. Those are commands that are a few bytes long and start with byte 255 (called IAC, interpret as command). When you connect to a telnet server, your client will interpret those and reply accordingly, all automatically in the background, without showing you those commands.

Although not specific to telnet, a telnet server can also send ANSI escape sequences. Those are used for colors, bold, cursor positioning, ... A telnet client will also interpret those (or just pass them on to the terminal emulator you're using, like xterm).

If you want something "lower-level" than telnet, which won't interpret telnet options and actually display what you get, you may want to take a look at netcat

share|improve this answer
add comment

Telnet has no particular codes, all it does is opening a TCP socket to the remote host, and then sends it the input from stdin and prints the responses to stdout.

If you run

$ telnet stackoverflow.com 80

you get a command prompt where you can send text or bytes to a remote server and view the answers in real-time.

share|improve this answer
add comment

Telnet is just an easy interactive way to open a TCP connection to a listening socket. Because the telnet client blindly sends what you type to that socket, it can theoretically emulate any other protocol on top of TCP. Actually the fact that non printable chars are interpreted by the keyboard driver is the only limit.

HTTP does not use non printable chars except to delimit between the HTTP header and the body with two consecutive "line breaks" (i.e. a "blank line").
Please note that I'm not talking about the HTML body tag here, but the payload (e.g SOAP body).

No magic here basically.

Let's see the dynamic of things.
HTTP supports a number of commands like GET, POST, PUT etc... Each command has its syntax and there is an associated response with an agreed upon syntax and well defined error codes. When you connect to an HTTP server using telnet, you open the socket connection and the server forks a thread to manage the dialog with your client. You can then mimic a browser by typing the command that the browser would send. Each time you strike the CR key, the client submits the line to the server. If a command contains several lines, you can enter several lines, each of them corresponding to a line of the command header. Once you strike two CR in a row (i.e. an empty line), the command header is deemed complete by the server and the response is put together and sent back to your client. Because a telnet client's life goal is to echo received characters (unless told otherwise), then you can see the response header and body on your terminal window. Telnet stops there. A browser would render the HTML (if the response is an HTML page).
I hope that clarifies it all.

share|improve this answer
add comment

You can query a http server using telnet, I often do as a quick and nasty test telnet doesnt send any codes. it just makes the connection, but there are telnet specific codes you can send if you require

do this...

telnet server.com 80
GET http/1.0 /^m
host: server.com^m

... servers response

^m = enter key

I have done this is done in linux in windows it may be different.


share|improve this answer
Ok if I understand correctly Telnet client just creates socket connection and wait to for server to send some Telnet specific stuff. If server sends some thing it will react with specific codes to negotiate NVT stuff.If not it will just hang there until user types some thing to relay it to the server. –  chamibuddhika Jan 19 '11 at 15:18
As I understand it it depends on how you start telnet. if you start it with a host and optional port like I have done above it connects straight to the host and waits for commands (what you type) if you start it without a host it goes into command mode. take a look here linux.about.com/od/commands/l/blcmdl1_telnet.htm and for windows here technet.microsoft.com/en-us/library/c.aspx I havent been able to get the example above to work on windows, so I'll try and find out why not. –  DeveloperChris Jan 19 '11 at 22:29
linux.about.com/od/commands/l/blcmdl1_telnet.htm solved my issue. It is dependant on how you start Telnet client as you mentioned eventhough I found the Linux client not having that particular switch. –  chamibuddhika Jan 21 '11 at 15:36
add comment

Your Answer


