Using telnet to Help Debugging TCP-based Application

Telnet can refer to either the Telnet network protocol used to provide a bidirectional interactive text-oriented communication facility or the telnet program that implements the client side of the Telnet protocol. This post focuses on the telnet program.

Telnet doesn’t encrypt data sent (including password) over the connection, so it’s not secure. SSH has been widely used to replace telnet for remote login/control.

Telnet data are transmitted over TCP. Except some special characters, telnet can be used to establish a raw TCP connection. Because of this, telnet can be used to debug TCP-based applications, such as HTTP server, FTP server, etc. You can also use it to test TCP servers developed by yourself.

0. An Example of Using Telnet to Debug HTTP

We can issue HTTP request to a HTTP server and examine the response. Below is an example of using HTTP to connect to google web server.

telnet www.google.com 80
Trying 74.125.235.51…
Connected to www.l.google.com.
Escape character is ‘^]’.
GET /index.html HTTP/1.1

HTTP/1.1 302 Found
Location: http://www.google.com.sg/index.html
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=0cf4ad941da7097d:FF=0:TM=1323450677:LM=1323450677:S=4CGr4KBGwGHxlgql; expires=Sun, 08-Dec-2013 17:11:17 GMT; path=/; domain=.google.com
Date: Fri, 09 Dec 2011 17:11:17 GMT
Server: gws
Content-Length: 232
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv=”content-type” content=”text/html;charset=utf-8″>
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF=”http://www.google.com.sg/index.html”>here</A>.
</BODY></HTML>

1. An Example of Using Telnet to Debug a Simple TCP Server

If we build a TCP server application, we can use telnet to test it. We use the TCP server program here for illustration. Below is the execution of running the server on port 12345 and using telnet to connect to it.

At server:

./server 12345
connection accepted
received: hello world
sent: hello world
received: hello roman10
sent: hello roman10
received: telnet sample
sent: telnet sample
received: for simple tcp server
sent: for simple tcp server
received: we’re going to finish
sent: we’re going to finish

At another terminal running telnet:

telnet 127.0.0.1 12345
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
hello world
hello world
hello roman10
hello roman10
telnet sample
telnet sample
for simple tcp server
for simple tcp server
we’re going to finish
we’re going to finish
Connection closed by foreign host.

Alternatives to Telnet

Telnet can be used as raw TCP client at many cases, but there’re some special characters telnet cannot handle like raw TCP client. Fortunately, there’re true raw TCP clients can be used as alternatives to telnet, including netcat and socat on Linux/UNIX, and PuTTY on Windows. We can use similar method to debug TCP-based applications using those programs.

Reference:
Telnet on Wikipedia: http://en.wikipedia.org/wiki/Telnet