Netcat UDP File Transfer?

Is there any way to send a file (picture or video) using Netcat and UDP. It defaults as TCP, but I would like to send using UDP. I tried simply adding -u to the nc command, but that didn't work. Here are the two commands I'm using:

cat File.jpg | nc -u -l 777
nc -u 192.168.x.x 777 | pv -b > newfile.jpg

I used my IP address for x.x, and the corresponding file on my PC. I am also using Ubuntu.

Thanks for any assistance!


Solution 1:

Try it like this:

nc -u -l 7777 > newfile.jpg #on the destination machine
cat file.jpg | nc -u 192.168.x.x 7777 #on the source machine

Usually you want the machine getting the file to "listen" (run that first), and when it's listening, send the data over udp. UDP does not have a 'handshake' sequence, and packets are sent immediately, even if noone is listening*.

*sometimes you get an ICMP packet, that the port is closed (unreachable), but you cannot depend on that (firewalls etc.)

Solution 2:

I think this question must be answered as follows: Yes, there is a way of sending a file with Netcat over UDP. However, it is not possible to reliably receive this file on the destination host.

If you want to have a usable file on the destination host, look for another solution.

Solution 3:

Using the TCP option does not guarantee that your file will be received intact.

TCP only provides 16 bits of error protection. That means that 1 in 65,536 transmission errors will get through.

To safely transmit files between systems you should calculate a hash of the entire file before sending and validate it after reception.

If you are doing that, sending via UDP is totally valid. You want to have a very low probability of transmission errors, however. Do not attempt this over a busy WiFI network, or to a computer on the other side of the continent. But within a building through a wired network you will encounter very few errors.