Temporary TCP tests often involve several clients connecting one after another. An OpenBSD Netcat listener can stay open between connections, so each test reaches the same receiving terminal.

Keep-open mode waits for a new TCP connection after the previous client disconnects. It handles clients in sequence, so a client that leaves its connection open can delay the next test.

Both terminals must use OpenBSD Netcat, available as netcat-openbsd on Ubuntu. The local-only listener accepts connections from the same machine without encryption or authentication, so it suits temporary tests with nonsensitive text.

Steps to keep a Netcat listener open for multiple connections:

  1. Start a keep-open listener on the loopback address in the first terminal.
    $ nc -lk 127.0.0.1 9000

    The -l option listens, and -k accepts another connection after the previous one finishes. No output appears until a client sends data. If port 9000 is already in use, choose an unused port for both the listener and clients. If nc rejects -k, the installed variant may not be OpenBSD Netcat.
    Related: How to install Netcat on Ubuntu

  2. Send a first message from a second terminal on the same machine.
    $ printf 'first connection\n' | nc -N 127.0.0.1 9000

    The -N option shuts down the client's sending side after printf finishes, allowing the connection to end. The client returns to the shell prompt without printing a reply.

  3. Check that the listener terminal displays the first message.
    first connection

    The listener stays running without returning to a shell prompt.

  4. Send a second message from the second terminal without restarting the listener.
    $ printf 'second connection\n' | nc -N 127.0.0.1 9000
  5. Confirm that the original listener displays both messages while remaining open.
    first connection
    second connection

    The second line comes from a separate client connection, proving that the listener accepted another client after the first disconnected. A returned shell prompt or a failed second connection means the listener did not remain open. When testing is finished, Ctrl-C in the listener terminal stops it.

Share this guide