What netcat does
A TCP connection is a two-way byte stream. Netcat makes one end of that stream and attaches it to a file descriptor. In connect mode it opens a connection to a host and port, sends the bytes it reads from standard input, and writes the bytes it receives to standard output. In listen mode it waits for one connection and does the same on the other side.
Because it has no protocol of its own, netcat is a good check for whether a port accepts connections and what it sends first. Many services announce themselves with a greeting line, which you can see with netcat before you know the protocol.
Listening on a port
The BusyBox version accepts the -l flag to listen and -p to choose the port. Listening blocks until a client connects. To keep the example safe, the listener in this unit runs under the timeout command, which stops it after a set number of seconds even if no client arrives.
timeout 5 busybox nc -l -p 9124 > received.txt &The listener runs in the background and writes whatever it receives into received.txt. The timeout stops it after five seconds, so a forgotten listener cannot hold the port open for long.
Sending a line
A client connects by giving the host and port. The text to send comes from standard input, typically from printf or from a file. When standard input reaches end of file, netcat closes the connection, which lets the server finish.
printf "hello\n" | busybox nc 127.0.0.1 9124Run the listener first and then the client, in that order, so that the port is open when the client connects. The listener then writes hello into the file. If the listener started after the client, the client would fail with connection refused.
Netcat has no authentication and sends everything in clear text. Never run an open listener on a machine that other people can reach, and never send passwords or keys through it.
Testing a port without sending data
To test whether a port is open, you can try a connection and immediately close it. Bash does this without any extra program, using the /dev/tcp path from the sockets unit. The redirection either succeeds or fails, and the status tells you which.
bash -c 'exec 3<>/dev/tcp/127.0.0.1/9999' 2>/dev/null; echo "status=$?"The status is 1 when nothing listens on port 9999. The message about connection refused goes to standard error, which the redirection hides here so the status line is easy to read.
Netcat and the other tools
The full netcat programs have more options, such as keeping the listener open for many clients and sending files in both directions. Their options differ between versions. Check the help of the version installed on your machine rather than copying flags from another system. The BusyBox nc on the lab machine supports the listen and connect forms shown here.
Hands-on: a one-line exchange
The lab asks you to start a listener, send the word hello through it, and save what the listener received. The solution runs the same steps, so you can compare. The important habit is starting the listener before the client.
timeout 5 busybox nc -l -p 9124 > received.txt &
sleep 1
printf "hello\n" | busybox nc 127.0.0.1 9124
sleep 1
cat received.txtThe final line prints hello, which is the byte stream that crossed the loopback connection.