The example in the previous section can be expanded to build a basic data transfer model. Any information input into one end of the connection is output to the other end, and input and output can be easily captured in order to emulate file transfer.
Start by using nc to listen on a specific port, with output captured into a file.
On a given machine, start listening over any port (any port over 1024 without needing su privileges). Here we arbitrarily chose port 1120:
$ nc -l -p 1120 > filename.out
The -l tag says we want to listen and the -p tag specifies the port.
Using a second machine, connect to the listening nc process, feeding it the file which is to be transferred.
On another machine connected to the same local network, connect to the first machine. If the first machine’s hostname is “second” we do:
$ nc second 1120 < filename.in
$ nc host.example.com 1120 < filename.in
After the file has been transferred, the connection closes automatically.