IDUDP问题(100分)

  • 主题发起人 主题发起人 bluebug
  • 开始时间 开始时间
B

bluebug

Unregistered / Unconfirmed
GUEST, unregistred user!
利用IDUDP如何接收文件,并在RXRICHEDIT上显示?

已经实现用IDUDPCLIENT传送文件(文字和图片混合在一起),现在问题是如何通过 IDUDPSERVER接收大的文件并在RXRICHEDIT上显示?
直接用
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
begin
inherited;
rxrichedit2.Lines.LoadFromStream(AData);
end;
可以接收小于4K的文字加小图标,超过4K就是一些大量的字符.
是不是要分段接收?如何用内存流写这样的代码?


分在http://www.delphibbs.com/delphibbs/dispq.asp?lid=3583049
 
代码如下:
发送:
procedure TForm1.Button1Click(Sender: TObject);
var
SendStream: TMemoryStream;
ABuffer: Pointer;
ReadBytes,l:integer;
begin

SendStream := TMemoryStream.Create;
rxrichedit1.Lines.SaveToStream(SendStream);

GetMem(ABuffer, SendStream.Size);
SendStream.Position :=0;
l:=SendStream.Size ;
repeat
ReadBytes:= SendStream.Read(ABuffer^,4096);
// SendStream.ReadBuffer(ABuffer^,ReadBytes);// SendStream.Size);
IdUDPC.SendBuffer(ABuffer^,readbytes);// SendStream.Size); // SizeOf(ABuffer));
until ReadBytes<4096;

// sendstream.ReadBuffer(abuffer^,sendstream.Size );

//IdUDPC.SendBuffer(abuffer^,SendStream.Size);
SendStream.Free;
end;

接收:
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
ReceStream: TMemoryStream;
ABuffer: Pointer;
ReadBytes,l:integer;
begin
inherited;

GetMem(ABuffer, AData.Size);
ReceStream := TMemoryStream.Create;
l:=adata.Size;
repeat
ReadBytes:=adata.Read(ABuffer^,4069);
ReceStream.WriteBuffer(ABuffer^, ReadBytes);
until readbytes<> 4069;

recestream.Position :=0;

rxrichedit2.Lines.LoadFromStream(recestream);
ReceStream.Free;
end;

小于4K的字符或BMP没问题,大于4K的就显示一堆字符,请高手帮忙
 
m没人回答,也不让我删除?
 
自己写个协议,否则不行
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
767
DelphiTeacher的专栏
D
D
回复
0
查看
809
DelphiTeacher的专栏
D
后退
顶部