0

我正在使用 Delphi Xe-3 indy 10 进行 udp 文件传输。我以小块的形式传递文件,在传输文本文件以外的更大文件时遇到问题。

在客户端,当我调试文件大小保存错误时,我无法找出原因。下面是客户端和服务器代码,客户端发送文件到服务器。(我使用 udp 是因为我正在研究 Reliable udp。)

客户端代码(发送功能)中提到了问题。

服务器:

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;

客户:

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

1 回答 1

1

Indy 有TIdTrivialFTPTIdTrivialFTPServer组件。 TFTP是一种基于 UDP 的文件传输协议。您应该考虑使用它而不是创建自己的自定义协议。

于 2013-11-04T17:07:03.580 回答