Netcat UDP File Transfer?

19,519

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.

Share:
19,519

Related videos on Youtube

Vinnie
Author by

Vinnie

Updated on September 18, 2022

Comments

  • Vinnie
    Vinnie over 1 year

    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!

    • Admin
      Admin over 11 years
      @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.
  • wfaulk
    wfaulk about 12 years
    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)
  • mulaz
    mulaz about 12 years
    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.).
  • Oliver
    Oliver about 12 years
    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.
  • Catherine MacInnes
    Catherine MacInnes over 8 years
    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.