title: UDP File Transfer Using Delphi Indy 10
body: <p>I am using Delphi Xe-3 indy 10 for udp file transfer . I am passing file in small chunks and I am facing problem while transfering bigger and files other than text files.</p>
  
  <p>In client when I debug the size of the file is saved wrong ,I am unable to figure out the reason. Below is the client and server code, Client send files to Server.
  (I am using udp because I am working on Reliable udp.)</p>
  
  <p>Problem is mentioned in the client code(send function).</p>
  
  <p>Server:</p>
  
  <pre><code>procedure TForm2.serverUDPRead(AThread: TIdUDPListenerThread;
    AData: array of Byte; ABinding: TIdSocketHandle);
    var
  
    str , len :string;
    size : word;
    index : word;
  
  begin
  
     setlength(b,length(Adata));
     move(Adata[0],b[0],length(Adata));              // convert array of byte to tidbytes
  
     index:=0;
     setlength(Ext,b[index]);                      // get filename length
     setlength(File_data , b[index+1]);             // get file length
     index:= index+2;
     move(b[index],Ext[1],length(Ext)*2);           // copy filename
     index:= index+length(Ext)*2;
     move(b[index],File_data[0],length(File_data));      // copy file
  
     save.Visible:= true;
     //progressbar1.Visible:= true;
     progressbar1.Position
     memo1.Lines.Add('Receving file...-&gt;');
  
  end;
  
  procedure TForm2.SaveClick(Sender: TObject);
  var
  buttonSelected : Integer;
  content:integer;
  begin
  
  savedialog1.FileName := Ext;            // save file name
  if savedialog1.Execute then            // save dialog opens
     try
      if FileExists(SaveDialog1.FileName) then
       begin
       if MessageDlg('Do you want to overwrite the existing file ?',
       TMsgDlgType.mtConfirmation,mbYesNo,0) = IDNO then
         begin
           exit;
         end
       end;
  
      Strm:=TFileStream.Create(Ext,fmCreate);
      Strm.Position:=0;
      //progressbar1.Value:= 100*length(File_data)/content;                                                // set position to start
      WriteTIdBytesToStream (Strm,File_data,length(File_data),0);      // write bytes data to stream
      memo1.Lines.Add('File transmission complete...');
      finally
      strm.Free;
      save.Visible := false;
      progressbar1.Visible := false;
      end;
  
  end;
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>procedure TForm1.LoadClick(Sender: TObject);
  begin
  
  //openDialog1.InitialDir := GetCurrentDir;
  
  if openDialog1.Execute then
  
  begin
  Edit1.Text     := OpenDialog1.FileName;
  memo1.Lines.Add (Opendialog1.FileName);
  
  Filename := ExtractFileName (Opendialog1.FileName);
  Mem := TFileStream.Create(OpenDialog1.FileName,fmOpenRead); //connects the client
  
  end;
  
  {finally
  opendialog1.Free;
  end;
      }
  
  end;
  
  procedure TForm1.SendClick(Sender: TObject);
  var
  size : word;
  index : word;
  begin
  index := 0;
   try
     Posi:=0;
     While Posi&lt;Mem.Size do
     begin
     Len:=1024;
     if Mem.Size-Posi&lt;1024 then
     begin
     Len:=Mem.Size-Posi;
     end;
     setlength(chunk,len);
     ReadTIdBytesFromStream(Mem,chunk,Len);
  
    size := length(chunk) + length(Filename)*2 + 2;  // it gets right sizes here , 
                                                     //chunk size is also correct
    setlength(File_data,Size);
    File_data[index] := length(Filename);
    index:= index+1;
    File_data[index]:= length(chunk);// its saved here ,value is reduced.
    index:= index+1;
    move(Filename[1],File_data[index],length(Filename)*2);
    index:= index+length(Filename)*2;
    move(chunk[0],file_data[index],length(chunk));
  
    client.SendBuffer('127.0.0.1',6002,File_data);
    Inc(Posi,Len);
    end;
  
    finally
    Mem.Free;
    Edit1.Text:='';
    Filename:='';
    end;
  
  end;
  
  procedure TForm1.FormCreate(Sender: TObject);
  begin
  client.Bindings.Add.Port:= 0;
  client.Active  := true;
  end;
  
  end.
  </code></pre>