Linux – Netcat UDP File Transfer

linuxnetcatudp

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!

Best Answer

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