Board index » delphi » Why I cann't receive the whole data using indy-TIdUDPClient and TIdUDPServer

Why I cann't receive the whole data using indy-TIdUDPClient and TIdUDPServer

I am writing a project(delphi6) with indy components:
TIdUDPClient and TIdUDPServer.In the project,I want to send a
file whose size is more then 8192 byte,when I add 'showmessage(inttostr(r.j));' in my project,i can receive all data sent,but
when I delete the 'showmessage(inttostr(r.j));' I only receive a
part of the file at the Server Port,I don't know why.Could you
please help me find what's wrong?Thanks.My project's source as
follows:

/////////////////////////////////////////

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
  IdUDPClient,IdSocketHandle, NMUDP, Psock, IdChargenUDPServer;

type
  Trecord=record
    i:integer;
    j:integer;
    a:array[0..8183] of char;
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    o: TOpenDialog;
    c: TIdUDPClient;
    s: TIdUDPServer;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure sUDPRead(Sender: TObject; AData: TStream;
      ABinding: TIdSocketHandle);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Share;

{$R *.dfm}
var
  rr:Trecord;
  mm:Tmemorystream;

procedure TForm1.Button1Click(Sender: TObject);
var
  r:Trecord;
  f:Tmemorystream;
  w,m:integer;
  readcount:longint;
begin
  o.Execute;
  s.Active :=true;
  c.Active :=true;
  f:=tmemorystream.Create;
  mm:=tmemorystream.Create;
  f.LoadFromFile(o.FileName);
  readcount :=8184;
  r.j :=readcount;
  showmessage(inttostr(f.Size));
  r.i :=f.Size mod readcount;
  while r.j=readcount do
  begin
    r.j:=f.Read(r.a,readcount);
    c.SendBuffer(r,sizeof(r));

    showmessage(inttostr(r.j));

  end;

end;

procedure TForm1.sUDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
begin
 adata.ReadBuffer(rr.i,4);
 adata.ReadBuffer(rr.j,4);
 adata.ReadBuffer(rr.a,rr.j);
 mm.WriteBuffer(rr.a,rr.j);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  mm.SaveToFile('c:\wu.txt');
end;

end.

 

Re:Why I cann't receive the whole data using indy-TIdUDPClient and TIdUDPServer


superwoo <super...@x263.net> schreef in berichtnieuws 3c1c0b9e$1_1@dnews...

Quote

> I am writing a project(delphi6) with indy components:
> TIdUDPClient and TIdUDPServer.In the project,I want to send a
> file whose size is more then 8192 byte,when I add

'showmessage(inttostr(r.j));' in my project,i can receive all data sent,but

Quote
> when I delete the 'showmessage(inttostr(r.j));' I only receive a
> part of the file at the Server Port,I don't know why.Could you
> please help me find what's wrong?Thanks.

[snipped]

UDP is _message_ oriented, not _stream_ oriented, as TCP. The maximum
message length is approx. 1500 bytes. If you overdo it, messages will be
lost.
Better use TCP, or write your own application-level "ack" scheme or some
such.

--
Regards,

Dirk Claessens
---------------------------------------------------------
http://www.claessens16.yucom.be
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

Other Threads