How to connect to a TCP service with Netcat

A TCP handoff can fail after the port opens if the client sends the wrong first bytes or waits for a service that expects input first. Netcat can open a raw TCP client connection so an operator can send a small protocol line and read the service response without a browser, SSH client, or application wrapper changing the exchange.

The basic client form is nc host port. Use it interactively when the request needs to be typed by hand, or pipe a short payload into nc when the same request should be repeatable. With OpenBSD Netcat, -N closes the network socket after standard input reaches end-of-file, which matters for services that wait for the client side to finish sending before they answer or close.

The examples use OpenBSD Netcat on Ubuntu 26.04 and a loopback test service on 127.0.0.1:9000. Replace the address, port, and payload with the target service, keep secrets out of plaintext TCP, and use Ncat or another TLS-capable client when the service expects TLS before application data.

Steps to connect to a TCP service with Netcat:

  1. Confirm the endpoint and the first bytes the service expects. Use a host or IP address plus a numeric port, and decide whether the protocol speaks first, waits for a command, or requires CRLF line endings.

    Use a port test first when the only question is whether the socket accepts TCP connections. Use a client exchange when bytes after the connection matter.

  2. Start a disposable local service in another terminal when you need a safe proof target.
    $ printf 'tcp service says hello\n' | nc -l 127.0.0.1 9000
    status

    Leave this terminal open before running the client. The status line appears only after the client in the next step connects and sends that payload.

    Skip this step when a real test service is already listening.

  3. Connect as the TCP client, send one request line, and close the socket after the payload reaches end-of-file.
    $ printf 'status\n' | nc -N 127.0.0.1 9000
    tcp service says hello

    -N is OpenBSD Netcat syntax for shutting down the network socket after stdin reaches EOF. If the installed nc rejects -N, use an interactive client and press Ctrl-D after typing the request, or use the equivalent close-after-EOF option for that Netcat variant.

  4. Open an interactive client when the service expects several lines or when prompts need to be watched by hand.
    $ nc service.example.net 12345

    Type the request lines exactly as the service expects, then press Ctrl-D when the client must close its sending side.

    Netcat does not add protocol headers, authentication, encryption, or retries; every byte typed or piped is what the service receives.

  5. Add verbose output and a timeout when the client does not return or when the failure signal needs to be saved.
    $ nc -v -w 1 127.0.0.1 9001
    nc: connect to 127.0.0.1 port 9001 (tcp) failed: Connection refused

    Connection refused means the host was reached but that port had no listener. A timeout usually points to an unanswered route, firewall, address, or service-binding issue. A blank successful connection can mean the service is waiting for client input.

  6. Switch to a protocol-specific client when the endpoint needs more than plaintext TCP bytes. Use a plaintext HTTP request only when the service speaks HTTP without TLS, and use Ncat with TLS support when the target expects a TLS handshake before application data.