unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle, IdAntiFreezeBase, IdAntiFreeze, IdUDPClient, ExtCtrls; const PackLen = 1024 * 4; type TForm1 = class(TForm) IdUDPServer1: TIdUDPServer; Button1: TButton; ProgressBar1: TProgressBar; Button2: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; IdAntiFreeze1: TIdAntiFreeze; Button3: TButton; Memo1: TMemo; Memo2: TMemo; IdUDPClient1: TIdUDPClient; Timer1: TTimer; procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } FMS: TMemoryStream; FFileName: string; FNowTime: DWord; FCLength: DWord; procedure ShowSpeed(Size: Integer); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle); var Buf: array [0..PackLen - 1] of char; cBuf: array [0..1] of char; Progress, Length: Integer; begin Fillchar(buf, PackLen, #0); AData.Position := 0; AData.Read(Buf, AData.Size); Memo1.Lines.Append('R'); if Buf[0] = 'F' then begin FFileName := Trim(Copy(Buf, 2, 50)); Length := StrToInt(Copy(Buf, 51, 50)); ProgressBar1.Max := Length; cBuf := 'C'; IdUDPClient1.Send(cBuf); //ABinding.SendTo(ABinding.PeerIP, 8818, cBuf, 1); FNowTime := GetTickCount; end else if Buf[0] = 'E' then begin FMS.Position := 0; FMS.SaveToFile(FFileName); FCLength := 1; FMs.Clear; end else begin FMS.Write(Buf, AData.Size); Progress := FMS.Position; Memo2.Lines.Append(IntToStr(AData.Size) + ' 【' + IntToStr(Progress) + '】【'+IntToStr(ProgressBar1.Max)+'】'); if AData.Size = 0 then Exit; ProgressBar1.Position := Progress; Application.ProcessMessages; FillChar(cBuf, 1, #0); cBuf := 'C'; IdUDPClient1.Send(cBuf); FNowTime := GetTickCount; Memo1.Lines.Append('S'); //FCLength := Progress; //ABinding.SendTo(ABinding.PeerIP, 8818, cBuf, 1); end; end; procedure TForm1.FormCreate(Sender: TObject); begin FMS := TMemoryStream.Create; FCLength := 0; end; procedure TForm1.FormDestroy(Sender: TObject); begin FMs.Clear; FMs.Free; end; procedure TForm1.Button1Click(Sender: TObject); begin IdUDPServer1.Active := True; IdUDPClient1.Active := True; end; procedure TForm1.Button2Click(Sender: TObject); begin FMs.Clear; end; procedure TForm1.ShowSpeed(Size: Integer); var M: string; S: Double; Blank: DWord; begin Blank := GetTickCount - FNowTime; if Blank = 0 then blank := 1; S := ((Size * 1.0 ) /1024 ) / (Blank * 1000); if S > 1024 then M := Copy(FloatToStr(S / 1024),1,5) + 'M/S' else M := Copy(FloatToStr(S),1,5) + 'K/S'; Label3.Caption := M; //FNowTime := GetTickCount; end; procedure TForm1.Button3Click(Sender: TObject); begin Memo1.Lines.Append(IntToStr(GetTickCount)); end; procedure TForm1.Timer1Timer(Sender: TObject); begin if (FCLength <> 1) and (ProgressBar1.Position<>ProgressBar1.Max) then begin if FCLength = ProgressBar1.Position then begin IdUDPClient1.Send('G'+IntToStr(FCLength)); end else begin FCLength := ProgressBar1.Position; ShowSpeed(FCLength); end; end; if ProgressBar1.Max > 0 then Label1.Caption := Copy(FloatToStr(ProgressBar1.Position * 100.0 / ProgressBar1.Max),1,5)+'%'; end; end.