Modular Computers the manual GitHub

nc

nc: send and receive over TCP or UDP.

Synopsis

nc [-u] [-b] [-p port] address port
nc [-u]
-l [-p] port

Description

nc (netcat) sends what it reads from standard input over the network, through the computer’s network cards, and writes what comes back to standard output. address is a network card’s address (its id, as lsdev -i shows it, with or without its id: prefix).

Over TCP (the default) it connects to port at address, or with -l listens on port and takes one connection. Then it sends what it reads and writes what comes, until the other side ends; at the end of its input it says it’s done sending, so the other side reads its end.

With -u, UDP: each read (a line, from a terminal) is a datagram to port at address, or broadcast for every card the computer’s cards reach (a wired card’s cables, a WLAN card’s range). What comes back to its port is written out. It ends when standard input ends. With -l, it writes each datagram that comes, and sends what it reads to whoever sent the last one, until it’s stopped (^C).

Options

-u
UDP, not TCP.
-l
Listens on port.
-p port
The port it sends from (or listens on, with -l). Without it, a free one is given (32768 to 60999).
-b
Taken, and needed for nothing: mcos sends to broadcast without SO_BROADCAST set.

Exit status

0 when it’s done; 1 for a bad option or port, or when it can’t connect or send (no network card: “Network is unreachable”; no card took it: “No route to host”; nothing listening: “Connection refused”).

Examples

nc -l 7 > /tmp/heard
nc 1b9c0e-6f0c-4e0f-9a43-7d2f3c1a5e88 7 < notes
nc -ul 7 > /tmp/heard
echo hello | nc -u 1b9c0e6a-6f0c-4e0f-9a43-7d2f3c1a5e88 7
echo all | nc -ub broadcast 7

Notes

mcos has UDP and TCP but no IP: an address is a network card’s, and the network is what the cards reach, with nothing routed between networks (a wired card and a WLAN card are two networks). A card never drops a frame: the other card takes it, or the sender hears why not at once (“No buffer space available”: it’s busy, its queue full or no program taking data there; “No route to host”: no card there in reach), and nothing tries again but the software. So a datagram that’s sent reached the other card; it’s lost there only when no program has its port, or its socket’s queue is full, as UDP’s are. TCP loses nothing: it tries again what wasn’t taken, or wasn’t acknowledged. A datagram or a connection to the computer’s own card, or a broadcast, reaches its own programs too.

GNU and OpenBSD netcat over UDP stay after standard input ends, until -q or -w says; this one ends at once, as with -q 0. Over TCP it says it’s done sending at the end of its input, as OpenBSD’s -N does.

A network card can turn its computer on while it’s off (Wake-on-LAN): a frame for it holding its wake message, anywhere in the data, presses the power button. A program sets the message (an empty one: none) with the card’s own method, an ioctl on its /dev file:

local sys = require("sys")
sys.ioctl(sys.open("/dev/network0"), "setWake", "letmein")

Then, from another computer: echo letmein | nc -ub broadcast 9 (port 9, discard, is the usual one; any will do). Interfaces are named as Linux’s: eth0 for the first wired card, wlan0 for the first WLAN card.

See also

lsdev(1)