Testing TCP and UDP servers with Netcat and Telnet

When it comes to testing TCP/UDP servers, netcat and telnet are two very useful tools. In this article, we will explore how to use these tools to create test clients and servers.

Testing TCP Server with Netcat and Telnet

First, let’s create a TCP server using netcat. We can do this by running the following command:

$ nc -l localhost 8080

This will start a TCP server on port 8080 and listen for incoming connections. Now we can connect to this server using a telnet client by running:

$ telnet localhost 8080

Once connected, we can send and receive messages from the server. For example, we can send the message “Hello, world!” by typing it into the telnet client and pressing Enter:

Hello, world!

The server should receive this message and display it on the terminal where it is running. You can also, in the server (netcat) terminal type and send messages from the server to the client.

Testing UDP Server with Netcat

To test a UDP server using netcat, we first need to create the server. We can do this by running the following command:

$ nc -u -l localhost 8080

This will start a UDP server on port 8080 and listen for incoming datagrams. Now we can send datagrams to this server using netcat by running:

$ echo "Hello, world!" | nc -u localhost 8080

This will send a datagram containing the message “Hello, world!” to the server. The server should receive this datagram and display it on the terminal where it is running.

Written on April 18, 2023