title: Transferring Large Data with UDP protocol
body: <p>I know TCP is better to send file but I have a homework about sending file via udp protocol . Is there any code example in C# or Java about sending file? </p>
  
  <p>I have server-client example to send and recieve message. I tried to send the file using the same way but could not succeed. I may need an algorithm to divide the file small parts and send them via datagram, and I have an idea to put "md5" of the part as header of the array to check if the packet is lost or not.</p>
  
  <p>Here is my try , my server side in java;</p>
  
  <pre><code>// 1. creating a server socket, parameter is local port number
  sock = new DatagramSocket(7777);
  
  // buffer to receive incoming data
  byte[] buffer = new byte[65536];
  DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
  byte []bigByteArray=new byte[1024*1024*1024*1024];
  // 2. Wait for an incoming data
  echo("Server socket created. Waiting for incoming data...");
  ByteBuffer target = ByteBuffer.wrap(bigByteArray);
  // communication loop
  
  while(true)
  {
    try
    {
      sock.receive(incoming);
      String s = new String(incoming.getData());
      if(s=="finish") break;
      target.put(incoming.getData());
    }
    catch(Exception e)
    {
  
    } 
  }
  fos.write(bigByteArray);
  fos.close();echo("RECIEVED");
  </code></pre>
  
  <p>and my client side;</p>
  
  <pre><code>String s;
  Path path=Paths.get("C:\\Users\\Toshiba\\Desktop\\aa.txt");
  byte[] data = Files.readAllBytes(path);
  
  
  try
  {
    sock = new DatagramSocket();
  
    InetAddress host = InetAddress.getByName("localhost");
  
    //take input and send the packet
  
    byte [] part;
    for (int i = -1; i &lt; data.length; i=i+100) 
    {
      if(sock.isConnected())
      {
        part=Arrays.copyOfRange(data,i+1,i+100 );
      }
      else i=i-100;
    }   
    byte [] f="finish".getBytes();
    DatagramPacket finalpac = new DatagramPacket(f ,f.length , host , port);
  
    sock.send(finalpac);
  }
  </code></pre>
  
  <p>Thank you in advance.</p>