06.12.2024

How To Use Netcat to Test TCP and UDP Connections

Netcat — powerful tool for testing TCP and UDP networks. It provides the ability to create client or server connections for test and debug various network protocols. Netcat is a flexible utility that finds various applications in network testing and analysis.

Let's delve deeper into its usage by examining how it can be employed to test TCP and UDP connections.

Testing TCP connections

Before you can start using the tool, you need to install it like this, for example in Debian:

sudo apt update
sudo apt install netcat

TCP (Transmission Control Protocol) is a protocol that provides reliable and orderly data transfer between network nodes. To test TCP connections with Netcat, follow these steps:

1. Start Netcat in server mode listening on a specific port:

nc -l port

Screenshot №1. Start nc as server

This command starts Netcat in server mode, which will listen for incoming connections on the specified port.

2.Start Netcat as a client establishing a connection to the server:

nc host port

Screenshot №2. Client connect

This command starts Netcat in client mode and establishes a connection to the specified host and port. Once a connection is established, both the server and the client can send and receive data. To send data to Netcat in client mode, simply type text and press Enter. The data will be sent to the server, which will accept it and display it in its terminal. Likewise, the server can send data to the client.

Thus, you can test and debug a TCP connection by sending data and observing its transmission and processing on both sides.

Testing UDP connections

UDP (User Datagram Protocol) is a protocol that provides data transfer without first creating a channel between network nodes. Use the following steps to test UDP connections:

1.Start Netcat in server mode, listening on a specific port for UDP data:

nc -ul port

Screenshot №3. Start nc to work in UDP mode

This command starts Netcat in server mode to listen for UDP data on the specified port.

2.Start Netcat as a client sending UDP data to the server:

nc -u host port

Screenshot №4. Connect to nc running as udp

This command starts Netcat in client mode and sends UDP data to the specified host and port.
Once the connection is established, you can check the transmitted data and its integrity. As with TCP, data can be sent both from the client to the server and vice versa.

Testing UDP connections allows you to test the reliability, throughput, and operation of the UDP protocol.

Checking if a port is available

Netcat can also be used to check if a port is available on a remote host. To do this, use the command:

nc -zv host port

Screenshot №5. Checking SSH port to opened on server

This command allows you to check the availability of the specified port on the specified host. If the connection is established successfully, the port will be considered open. Otherwise, if the connection cannot be established, the port will be considered closed.

File transfer for network testing

Netcat can also be used to transfer files over TCP or UDP connections in order to test network stability and throughput.

To start the server in the mode of receiving a file from the client to the server via a TCP connection, use the following command:

nc -l port > filename

Screenshot №6. Start nc as file receiver

This command starts Netcat in server mode, which accepts data and redirects it to the specified file.

Then, on the client side, you can send the file like this:

nc host port < filename

Screenshot №7. Sending file to server by client

In this case, Netcat will be used to transfer the data from the file to the client machine over the specified TCP connection.

Advanced options

Netcat supports many advanced options that allow you to control and configure connections. For example, with the "-s" flag, you can specify a specific outgoing IP address, and the "-p" flag allows you to select a specific port for the outgoing connection.

Netcat provides flexible and convenient options for testing TCP and UDP connections. It can be a useful tool for network professionals and developers when debugging and checking network protocols. Mastering and using Netcat will help you deepen your networking knowledge and become a more experienced networking professional.