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!

  • @Oliver: This is exactly the kind of answer that is of no practical use. I understand the point you try to make, but Mulaz gave the commands that were asked about. Whether or when to use TCP/UDP is the requester's decision, not the point of this post. – user140304 Oct 10 '12 at 17:15

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

  • 6
    Just to say it explicitly: UDP has absolutely no "reliability, ordering, or data integrity. Thus, UDP provides an unreliable service and datagrams may arrive out of order, appear duplicated, or go missing without notice. UDP assumes that error checking and correction is either not necessary or performed in the application, avoiding the overhead of such processing at the network interface level." (Wikipedia) – wfaulk May 6 '12 at 2:37

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.

  • The file will be recieved correctly 99+% of the time. Packet error/loss rates on a local network are very low. I know JPEG is a bad example, but sometimes, with some data, you don't need 100%, especially if you send it often (sensors, etc.). – mulaz May 6 '12 at 14:09
  • The question was not about a local network, nor about lossy data. It was about a normal file for which netcat and udp is not the way to go. – Oliver May 7 '12 at 5:44

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.

  • Please note that this is a very old question from 2012. It is generally best to avoid adding to old threads in order to keep the front page clear for recent queries. – Catherine MacInnes Feb 5 '16 at 0:01

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.