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
  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.


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/


  • 50011 is a TCP port number, you can choose your own;
  • 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.

  • Nice. How do I send the file if the client is windows? I tried in powershell.. This is how far I got. $file=Get-Content ./somefile; echo "filename10"$file | ./Netcat32 IP PORT. On the linux machine i get a file called " 'filename10'$'\r' " (without double quotes). The file contents are correct but the file name is off. – MyWays Oct 21 '18 at 19:08
  • @MyWays I cannot help you with this PowerShell code (I'm a Linux guy, I have never worked with PowerShell), but the issue looks like being caused by different line endings in Windows and in Unix. – Kamil Maciorowski Oct 21 '18 at 19:12
  • @MyWays Although proper code between read and cat in Linux can fix this. Give me few minutes. – Kamil Maciorowski Oct 21 '18 at 19:15
  • @MyWays My answer now addresses this. – Kamil Maciorowski Oct 21 '18 at 19:30
  • this is some wizardry right here. – MyWays Oct 21 '18 at 19:32

No, ordinary netcat has no such special commands. It doesn't interpret its input, and really it doesn't even know that its input/output is a file: the redirection is done by you shell and netcat doesn't bother looking.

So you need an additional program which does interpret received input and opens the output files on its own. In the end, you would practically reinvent tar. (Or cpio, or a dozen other similar formats.)

[client] tar -cf - fileXYZ | nc <ip> <port>
[server] nc -lp <port> | tar -xvf -

There are Windows builds of GNU tar, and Win10 has recently started including bsdtar. If neither is available, you'll have to create some protocol of your own – e.g. send the filename as the first line, followed by data; you'll need to write custom code for the sender and for the receiver.

Grouping multiple commands in parentheses as a single pipeline unit will help:

[client] (echo fileXYZ && type fileXYZ) | nc <ip> <port>
[server] nc -lp <port> | (read name; name=${name##*/}; name=${name%$'\r'}; cat > "$name")

But instead, I would set up Samba on the Linux machine and configure a public share, so that clients could simply copy fileXYZ \\<ip>\incoming\fileXYZ.

  • "…create some protocol of your own" – The OP's nick is MyWays. :D – Kamil Maciorowski Oct 21 '18 at 14:17
  • @grawity Thanks for the answer. I'll try. Please read my answer again as I've added an idea and tell me what do you think? – MyWays Oct 21 '18 at 14:18
  • @KamilMaciorowski Haha, what's that supposed to mean? – MyWays Oct 21 '18 at 14:18
  • @MyWays You may reinvent tar your way. – Kamil Maciorowski Oct 21 '18 at 14:19
  • its my way or the tarway, jk. Is my edit a good idea? – MyWays Oct 21 '18 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.