NND没人会,高人请进来纠正错误啊!!300分跪求!!richedit1中的图片传到richedit2里为什么是二进制流啊咋不是图片呢?(300分)

  • 主题发起人 主题发起人 shm83
  • 开始时间 开始时间
你先不用发送出,
就在本用richedit1保存成文件流,再在richedit2中加载文件流看看能不显示图形。
 
绝对有显示图形!!
哎没人会啊???问提啊,谁来纠正的啊?????????
procedure TForm1.IdUDPServer2UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
strm2:TMemoryStream;
buf:array [0..254] of char;
Str:String;
begin
strm2:=TMemoryStream.Create;
strm2.Clear;
AData.Seek(0,0);
AData.ReadBuffer(buf[0],adata.size);
RichEdit2.Lines.Add(buf);
end;它有问提啊,谁会的啊?????????
 
RichEdit 保存到流 Lines.SaveToStream(vStream);
接收流后 Lines.LoadFromStream(vStream); 不行? 图片等格式文本由 RichEdit内部处理了。
而用 Lines.Add(xx) -- 只能加纯文本内容。
 
那这个怎么改:
strm2:=TMemoryStream.Create;
strm2.Clear;
AData.Seek(0,0);
AData.ReadBuffer(buf[0],adata.size);
RichEdit2.Lines.Add(buf);这个怎么改
 
可以实现,但也还有点问题,原因不明!文中有说明
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit2, ComCtrls, IdUDPServer, IdBaseComponent,
IdComponent, IdUDPBase, IdUDPClient, IdSocketHandle, RxRichEd,
IdTCPServer, IdTCPConnection, IdTCPClient;

type
TForm1 = class(TForm)
btnSend: TButton;
IdUDPClient1: TIdUDPClient; // 没使用
IdUDPServer1: TIdUDPServer;
RichEdit1_1: TRichEdit;
btnOpen: TButton;
RichEdit2_1: TRichEdit;
RxRichEdit1: TRxRichEdit;
RxRichEdit2: TRxRichEdit;
IdUDPServer2: TIdUDPServer;
procedure btnSendClick(Sender: TObject);
procedure btnOpenClick(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
fReceiveMS :TMemoryStream; // 接收流
fbIsRecaive :boolean; // 是否正在接收
fiRecaiveSize,fiCurRecSize :Int64; // 将要接收总大小,已接收大小
end;

var
Form1: TForm1;

implementation

uses Math;

{$R *.dfm}

procedure TForm1.btnSendClick(Sender: TObject);
var
ms :TMemoryStream;
Buffer : array [0..1023] of Char;
iCount, iAllSize, iByteCount :Int64;
begin
ms := TMemoryStream.Create;
try
{IdUDPClient1.BufferSize := 8192;
IdUDPClient1.Host := '127.0.0.1';
IdUDPClient1.Port := 20001;
IdUDPClient1.Active := True;
IdUDPClient1.Send('StartSendData');} // 开始标志

IdUDPServer2.Send('127.0.0.1',20001,'StartSendData');
RxRichEdit1.Lines.SaveToStream(ms);
iAllSize := ms.Size;
//IdUDPClient1.SendBuffer(iAllSize,SizeOf(Int64)); // 大小
IdUDPServer2.SendBuffer('127.0.0.1',20001,iAllSize,SizeOf(Int64));

ms.Seek(0,soBeginning);
iCount := 0;
iByteCount := Min(ms.Size,SizeOf(Buffer));
while iByteCount >0 do
begin
ms.ReadBuffer(Buffer[0],iByteCount);
//IdUDPClient1.SendBuffer('127.0.0.1',20001,Buffer,iByteCount);
//IdUDPClient1.SendBuffer(Buffer,iByteCount);

IdUDPServer2.SendBuffer('127.0.0.1',20001,Buffer[0],iByteCount);
Inc(iCount,iByteCount);
iByteCount := Min(iAllSize-iCount,SizeOf(Buffer));
end;
// Todo: 分段数据是成功的,可是接收次数有时与这里不一致,搞不懂呀,搞不懂。
// 有时成功,有时不行!!!
finally
ms.Free;
end;

end;

procedure TForm1.btnOpenClick(Sender: TObject);
begin
with TOpenDialog.Create(nil) do
try
if Execute then
RxRichEdit1.Lines.LoadFromFile(FileName);
finally
Free;
end;
end;

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
iSize :integer;
Buffer:array [0..1023] of Char;
begin

if Not ((ABinding.IP = '127.0.0.1') and (ABinding.Port =20001)) then Exit;

iSize := AData.Size;
if (Not fbIsRecaive) and (iSize = 13) then // 开始标志
begin
AData.ReadBuffer(Buffer,iSize);
if StrLIComp(Buffer,'StartSendData',iSize)=0 then
begin
//fReceiveMS.Clear;
//fReceiveMS.Size := 0;
if Assigned(fReceiveMS) then FreeAndNil(fReceiveMS);
fReceiveMS := TMemoryStream.Create;
fiRecaiveSize := 0;
fiCurRecSize := 0;
fbIsRecaive := True;
Exit;
end;
end;

if fbIsRecaive and (iSize >0) then
begin
if fiRecaiveSize =0 then // 大小
begin
AData.Read(fiRecaiveSize,SizeOf(Int64));
Exit;
end;
AData.ReadBuffer(Buffer[0],iSize);
if Not Assigned(fReceiveMS) then Exit;
fReceiveMS.WriteBuffer(Buffer[0],iSize);
Inc(fiCurRecSize,iSize);
if fiCurRecSize >= fiRecaiveSize then // 结束
begin
fbIsRecaive := False;
fReceiveMS.Seek(0,soBeginning);
RxRichEdit2.Lines.LoadFromStream(fReceiveMS);
//fReceiveMS.Clear;
fiRecaiveSize :=0;
fiCurRecSize :=0;
FreeAndNil(fReceiveMS);
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//fReceiveMS := TMemoryStream.Create;

with IdUDPServer1.Bindings.Add do // 接收绑定 IP/Port
begin
IP := '127.0.0.1';
Port := 20001
end;
IdUDPServer1.ThreadedEvent := True;
IdUDPServer1.Active := True; // 接收

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(FreceiveMS) then FreeAndNil(fReceiveMS);
end;

end.
 
其实你没有认真看代码.我刚才也在测试,如果数据量大,这样使用udp可能会有问题.而且,只要出现一位数据错误,RichEdit显示的内容就不对.你可以多运行几次传输,数据会正确的.
现在需要的可能是怎么保证数据传输正确.或者使用tcp协议.
 
有时成功,有时不行,是协议本身如此.
两种方法,一是使用tcp协议.二是加上数据校验,每个包发送完成后等待服务端发回校验信息,如果正确才进行下一个包的发送.但是这样会引发另外的问题,就是需要考虑超时,否则如果服务端没有信息返回,系统会死的.
就上面的代码,加上数据传输校验,应该就可以
 
还是没人会啊!!!
可悲!
 
接受答案了.
 
后退
顶部