this slowpoke moves

Send File UDP

uses IdUDPClient, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle

var
  Form1: TForm1;
  get_accept: boolean = false;
  ServerStream : TFileStream;
  first_       : boolean=true;
  get_buffers  : integer;
  get_buf_num  : integer;
  
//

procedure  SendProcedure(FStream: TFileStream; UDPClient: TIdUDPServer;
           StatusLabel :Tlabel; ProgressBar :TProgressBar);
var
  i,j,count: integer;
  buf : array[1..40000] of Char;
begin
  i:=round(FStream.Size/40000);
  if (i*40000<FStream.Size) then i:=i+1;
  ProgressBar.Max:=i;
  UDPClient.Send('localhost',1151,'count_buf:'+IntToStr(i)+'#');
  Application.ProcessMessages;
  for j:=1 to  i do
  begin
    if i=j then
    count:=FStream.Read(buf,FStream.Size-((i-1)*40000))
    else count:=FStream.Read(buf,length(buf));
    UDPClient.SendBuffer('localhost',1152,buf,count);
    Form1.Label1.Caption:='Send ¹ '+IntToStr(j);
    Form1.ProgressBar1.Position:=j;
    while get_accept=false do Application.ProcessMessages;
    get_accept:=false;
  end;
end;

procedure TForm1.UDPServerUDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
var
  str_buf : array[1..20] of Char;
begin
  if first_=true then
  begin
    AData.Read(str_buf,length(str_buf));
    if copy(str_buf,1,10)='count_buf:' then
    get_buffers:=StrToInt(copy(str_buf,11,pos('#',str_buf)-11));
    if SaveDialog1.Execute then
    begin
      first_:=false;
      ServerStream:=TFileStream.Create(SaveDialog1.FileName,fmCreate);
    end;
  end else
  begin
    get_buf_num:=get_buf_num+1;
    ServerStream.Position:=ServerStream.Size;
    ServerStream.CopyFrom(AData,AData.Size);
    IdUDPServer2.Send('localhost',1151,'accept');
    if get_buffers=get_buf_num then
    begin
      ServerStream.Free;
    end;
  end;
end;

procedure TForm1.UDPClientUDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
begin
  get_accept:=true;
end;
Datei laden :
procedure TForm1.Button1Click(Sender: TObject);
var
  FStream : TFileStream;
begin
  if OpenDialog1.Execute then Edit1.Text:=OpenDialog1.FileName;
  FStream:=TFileStream.Create(OPenDialog1.FileName,fmOpenRead);
  Label2.Caption:=IntToStr(Round(FStream.Size/1024));
  Label1.Caption:=FloatToStr(FStream.Size/40000);
  FStream.free;
end;
Datei Senden :
procedure TForm1.Button2Click(Sender: TObject);
var
  FStream : TFileStream;
begin
  if not FileExists(Edit1.Text) then Exit;
  try
    FStream:=TFileStream.Create(Edit1.Text,fmOpenRead);
    SendProcedure(FStream, IdUDPServer1, Label3, ProgressBar1);
    finally
    FStream.Free;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate