Simple Network Server Part 1

Created on: 2015-11-06 19:00:00 -0500

Categories: Network Applications


The majority of my work entails developing network centric applications. Often I need to experiment with various technologies to see how they work. One way of testing certain technologies is using a simple server to listen for network connections and display the contents of the packet to the screen. This helps me in understanding what is being sent from the client to the server. Based on my experience there are a few ways to accomplish this. In this quick tutorial I will go over one of 3 methods I use to do this.

The simple server we will be using today simply prints the contents of the packet to the echo server’s console. This example can easily be extended to handle more debugging situations. We will use Fedora 20 as the OS in this tutorial. But Methods 2 & 3 should work on a Windows system.

Method 1 using Netcat

If you are using any Linux system, there is a command line tool called Netcat (aka ncat or nc). At the same time we will install telnet to do some interactive communication with the server. We will also install the netstat command line tool to help us with this tutorial. To install Netcat and telnet run following command:

$ sudo yum install nmap-ncat telnet net-tools
# if you are using Fedora 21 or newer
$ sudo dnf install nmap-ncat telnet net-tools

Now the Netcat application is now available for you to use. The next step is to determine which IP Transport layer (TCP or UDP) and port number to use. In this example we will first create a server to accept TCP on port 8088. The port has been randomly chosen and no other applications are currently using this port.

If you are unsure what TCP ports are available to use you can run the following command:

$ netstat -tln

You will get a list of active applications listening to certain ports. Under the column ‘Local Address’, is the list of used ports. The convention is usually listening IP address then semicolon then port number.

Lets start the TCP echo server on port 8088 listening on 127.0.0.1 with the following command:

$ nc -l 127.0.0.1 8088 --exec "/bin/cat"

From another console window issue the netstat command again and check if the application is now listening to port 8088.

$ netstat -tln
Active Internet connections (only servers)
tcp        0      0 127.0.0.1:8088          0.0.0.0:*               LISTEN

Now run a telnet session to the server and type in some text.

$ telnet localhost 8088
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
This is a test to see if this works
This is a test to see if this works

You will notice that the text you enter will be sent back to you through the telnet window. Now kill the Netcat session (by pressing Ctrl-C in the netcat console).

It should be noted that Telnet uses TCP. If we need to test UDP we can accomplish that with the Netcat application itself. Lets restart the Netcat echo server but with the –udp flag and verify it is running with netstat listing UDP servers.

$ nc -l 127.0.0.1 8088 --udp --exec "/bin/cat"
#verify it has started using netstat
$ netstat -uln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
udp        0      0 127.0.0.1:8088          0.0.0.0:*    

Now lets send a UDP packet.

$ nc -4u -w4 127.0.0.1 8088
This is a test
This is a test
^C
$

If you want to use the echo server one on computer and a client from another you can simply substitute 127.0.0.1 with the IP address of the server. In the case of Fedora 20 and up, you may need to add a firewall rule to allow this. This can be done with the following command:

# to allow inbound UDP on port 8088  
$ sudo firewall-cmd --add-port=8088/udp
# to allow inbound TCP on port 8088
$ sudo firewall-cmd --add-port=8088/tcp

In my next tutorial I will show how to quickly set up Python to act as an echo server.