UDP senders transmit datagrams without establishing a connection first. On Linux, a Netcat UDP listener displays incoming data in a terminal, making it possible to check whether a local application sends the expected text.

Netcat prints received bytes without interpreting an application protocol. Text messages are easy to recognize, while binary datagrams may contain characters that do not display clearly in a terminal.

OpenBSD Netcat is available as the netcat-openbsd package on Ubuntu. A loopback listener accepts traffic only from the same machine, so a local test needs neither a firewall opening nor administrator privileges.

Steps to create a UDP listener with Netcat:

  1. Check whether UDP port 9001 is already in use.
    $ ss -lun 'sport = :9001'
    State Recv-Q Send-Q Local Address:Port Peer Address:Port

    Only the header appears when no UDP listener uses the port. If a socket row appears, use another unused port in both terminals.

  2. Create the UDP listener on 127.0.0.1:9001 in the first terminal.
    $ nc -u -l 127.0.0.1 9001

    The -u option selects UDP, and -l enables listening. No output appears while the listener waits for data. The loopback address limits access to local processes; a private-interface or all-interface bind allows other machines to send data to the terminal.

  3. Send a text datagram from a second terminal on the same machine.
    $ printf 'udp listener test\n' | nc -u -w 1 127.0.0.1 9001

    The sender exits after one second of inactivity. Its silent exit does not confirm delivery because UDP provides no delivery acknowledgement.

  4. Confirm that the first terminal displays udp listener test beneath the listener command.
    $ nc -u -l 127.0.0.1 9001
    udp listener test

    The received line shows that the listener accepted the datagram on the chosen address and port. The listener remains in the foreground; Ctrl+C stops it when it is no longer needed. The -w option does not impose a timeout on a listening socket. A basic UDP listener accepts data from its first sender; multiple senders require a keep-open listener.
    Related: How to keep a Netcat listener open for multiple connections

Share this guide