Receiving multiple files at once in Netcat without overwriting last file

netcat

1 machine listens(linux), while multiple clients(windows) send files to it on one and the same listening port. Netcat receives the files serially with the -k tag.

Listening machine: 
  nc -lp PORT -k > fileX
Clients: 
  nc IP PORT < file??

What I want is to receive multiple files from multiple sources WITHOUT them overwriting each other.

Please look at the previous commands. The listener machine with always write output to fileX, thus overwriting the previous client's file.

I want to somehow be able to change the output filename for every different client. Can the client send the string "file10" over netcat somehow and so tell the listener to output to > file10 ?

Thank you.

EDIT:
Came up with an idea:

This is automated via script:

  1. Firstly, listener sets output file to file.txt. (nc -lp PORT > file.txt)

  2. Client sends string (eg "file123") via netcat. (This will be the filename for the file that will be sent in the future.)

  3. Listener writes to file.txt, then reads file.txt, and starts a new netcat listener with output file > file123

  4. Finally, client sends data.

Best Answer

This other answer discusses the point of having your custom protocol/setup. I won't repeat it. I assume your own solution is what you want.

nc -k is not the best tool to receive "multiple files from multiple sources" because it will listen for another connection only after the current one terminates. This means it will receive multiple files but one by one, not in parallel. Your "multiple sources" will block one another.

socat with reuseaddr and fork may be better.

As a proof of concept let's create (in a quick and dirty way) our custom protocol. A file will be transferred as a stream that consists of:

  1. filename or path to use on the receiving side (without newlines);
  2. single newline (\n, LF, 0x0a) as a separator;
  3. binary data;
  4. EOF.

This is the receiving command:

socat TCP-LISTEN:50011,reuseaddr,fork SYSTEM:'read -r f && cat >"$f"'

(Edit) This is universal receiving command that strips trailing \r (if any) from filename (useful for working with Windows clients not entirely protocol-compliant):

socat TCP-LISTEN:50011,reuseaddr,fork SYSTEM:'read -r f && f="${f%$(printf "\r")}" && cat >"$f"'

(Edit ends here).

To send a file:

(echo "The new name.foo" && cat "./the file to send.bar") > /dev/tcp/192.168.22.33/50011

Notes:

  • 50011 is a TCP port number, you can choose your own;
  • 192.168.22.33 is the address of the server, change it to match your setup;
  • I used /dev/tcp/…/… syntax that works in Bash, pipe to nc if you want/need;
  • filename collisions are still the issue, you need some scripted logic (instead of plain cat) to resolve them;
  • SYSTEM has its limitations (see man socat); instead of passing some large script body to it, write the script to a file and run the file; you can also investigate EXEC;
  • our protocol (being quick and dirty) doesn't provide any way for a server to report errors (if any) or success to a client.

I tested this with Debian receiver and Ubuntu sender. At one moment three different connections were transferring three different files from two different IP addresses. After all the transfers completed, md5sum was used to verify if the copies were (most likely) the same as originals; they were.

Related Question