UDP File Transfer Using Delphi Indy 10
我正在使用 Delphi Xe-3 indy 10 进行 udp 文件传输。我以小块的形式传递文件,在传输更大的文件和文本文件以外的文件时遇到问题。
在客户端调试时文件的大小保存错误,我无法找出原因。下面是客户端和服务器代码,客户端发送文件到服务器。
(我使用 udp 是因为我正在研究 Reliable udp。)
客户端代码(发送函数)中提到了问题。
服务器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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; |
客户:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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 有