简体   繁体   English

使用Delphi Indy 10进行UDP文件传输

[英]UDP File Transfer Using Delphi Indy 10

I am using Delphi Xe-3 indy 10 for udp file transfer . 我正在使用Delphi Xe-3 indy 10进行udp文件传输。 I am passing file in small chunks and I am facing problem while transfering bigger and files other than text files. 我正在以小块形式传递文件,并且在传输更大的文件和文本文件以外的文件时遇到了问题。

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.) (我正在使用udp,因为我正在开发可靠的udp。)

Problem is mentioned in the client code(send function). 客户代码(发送功能)中提到了问题。

Server: 服务器:

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

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;

Client: 客户:

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<Mem.Size do
   begin
   Len:=1024;
   if Mem.Size-Posi<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.

Indy has TIdTrivialFTP and TIdTrivialFTPServer components. Indy具有TIdTrivialFTPTIdTrivialFTPServer组件。 TFTP is a UDP-based file transfer protocol. TFTP是基于UDP的文件传输协议。 You should consider using that instead of creating your own custom protocol. 您应该考虑使用它而不是创建自己的自定义协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM