Hints for debugging simple TCP applications

Summary of what you are going to install:

  1. ncat
  2. curl

Summary of the commands:

sudo apt-get install ncat
sudo apt-get install curl
echo "hello world" > /dev/tcp/localhost/11111
echo "hello world" | nc localhost 11111
curl -v http://yoururl... -H "Accept-Language: en-us" -H "Accept-Encoding: gzip, deflate" -H "Connection: Keep-Alive" -A "Mozilla/4.0 (compatible; MSIE5.01; Windows NT)" -H 'Accept:'

Case scenario 1: Fire and forget

You want to send simple raw data to a socket. You are not interested in displaying the response.

The following command will connect, write the message including a new line and then disconnect:

$ echo "hello world" > /dev/tcp/localhost/11111

If you do not want to include a new line at the end of the message,  you may use the following command:

$ echo -n "hello world" > /dev/tcp/localhost/11111

Case scenario 2: Send and receive

You want to send raw data to a socket. You are interested in displaying the response.

The following command requires netcat which may not be installed by default. You can easily install it with the optional command show below:

$ sudo apt-get install ncat
$ echo "hello world" | nc localhost 11111

As noted above, this will include a new line at the end of the message. If you do not want to include a new line you will have to use the -n flag of the echo as described in the previous scenario.

Case scenario 3: Send and receive

You want to send raw data to a socket. You are interested in displaying the response. Your server can handle new lines as delimiters of your messages.

In this case you can use telnet. Note that telnet is line buffered which means that it sends the message only when you press enter.

$ telnet localhost 11111
...

Case scenario 4: Send proper HTTP headers and receive

You want to send an HTTP header. You are interested in displaying the response.

The following command requires curl which may not be installed by default. You can easily install it with the optional command show below:

$ sudo apt-get install curl
$ curl -v http://yoururl... -H "Accept-Language: en-us" -H "Accept-Encoding: gzip, deflate" -H "Connection: Keep-Alive" -A "Mozilla/4.0 (compatible; MSIE5.01; Windows NT)" -H 'Accept:'

The program curl is a great utility with many flags available. Its needs time to master but it can construct almost any kind of http headers you can imagine.

The example above will construct and send the following headers:

GET yoururl... HTTP/1.1\r\n
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n
Host: www.tutorialspoint.com\r\n
Accept-Language: en-us\r\n
Accept-Encoding: gzip, deflate\r\n
Connection: Keep-Alive\r\n
\r\n

 

 

 

Leave a Reply