UDP如何发送和接收带有图片流的结构? ( 积分: 200 )

  • 主题发起人 主题发起人 车超
  • 开始时间 开始时间

车超

Unregistered / Unconfirmed
GUEST, unregistred user!
如:
type TTUDPHead=record
MsgType:String[2];
end;
type TBody=packed record
ss:TMemoryStream;
end;
type
TSdate=record
Head:TTUDPHead;
Body:TBody;
end;
发送
sd.Head.MsgType:='1';
sd.Body.ss := TMemoryStream.Create;
jpg := TJpegImage.Create;
jpg.Assign(Image.Picture.Bitmap);
jpg.CompressionQuality := 50;
jpg.JPEGNeeded;
jpg.Compress;
jpg.SaveToStream(sd.Body.ss);
jpg.Free;
IdUDPServer1.SendBuffer('192.168.1.88',8581,sd,sizeof(sd));
接收
BufferLen := AData.Size;
rr.Body.ss := TMemoryStream.Create;
rr.Body.ss.Size := BufferLen;
try
AData.ReadBuffer(rr,Bufferlen);
except
end;
jpg := TJpegImage.Create;
jpg.LoadFromStream(rr.Body.ss);
Image.Picture.Bitmap.Assign(jpg);
jpg.Free;
memStream.Free;
结果是错误的,请问应该怎么改才是正确的?
 
如:
type TTUDPHead=record
MsgType:String[2];
end;
type TBody=packed record
ss:TMemoryStream;
end;
type
TSdate=record
Head:TTUDPHead;
Body:TBody;
end;
发送
sd.Head.MsgType:='1';
sd.Body.ss := TMemoryStream.Create;
jpg := TJpegImage.Create;
jpg.Assign(Image.Picture.Bitmap);
jpg.CompressionQuality := 50;
jpg.JPEGNeeded;
jpg.Compress;
jpg.SaveToStream(sd.Body.ss);
jpg.Free;
IdUDPServer1.SendBuffer('192.168.1.88',8581,sd,sizeof(sd));
接收
BufferLen := AData.Size;
rr.Body.ss := TMemoryStream.Create;
rr.Body.ss.Size := BufferLen;
try
AData.ReadBuffer(rr,Bufferlen);
except
end;
jpg := TJpegImage.Create;
jpg.LoadFromStream(rr.Body.ss);
Image.Picture.Bitmap.Assign(jpg);
jpg.Free;
memStream.Free;
结果是错误的,请问应该怎么改才是正确的?
 
你用IdUDPServer1.SendBuffer('192.168.1.88',8581,sd,sizeof(sd));发送的只是sd没发送ss中的数据


发送
s := TMemoryStream.Create;
sd.Body.ss := TMemoryStream.Create;
jpg := TJpegImage.Create;
jpg.Assign(Image.Picture.Bitmap);
jpg.CompressionQuality := 50;
jpg.JPEGNeeded;
jpg.Compress;
jpg.SaveToStream(sd.Body.ss );
jpg.Free;
sd.Body.ss .Position:=0; //*********
s.Wirtebuffer(sd,sizeof(sd));
s.Wirtebuffer(sd.Body.ss ,sd.Body.ss .size);
IdUDPServer1.SendBuffer('192.168.1.88',8581,s.Memory^,s.size);
接收

BufferLen := AData.Size-rr.size;//rr的结构与sd相同
AData.ReadBuffer(rr,rr.size);
rr.Body.ss := TMemoryStream.Create;
AData.read(rr.Body.ss ,BufferLen);
rr.Body.ss.Position:=0;
jpg := TJpegImage.Create;
jpg.LoadFromStream(rr.Body.ss);
Image.Picture.Bitmap.Assign(jpg);
jpg.Free;
 
我试一下
 
不对啊,我是想整个结构都发过去啊,你只是单发流过来而已,Head呢,其实我还有许多其他的结构成员的,我知道单发图象流可以,可发整个结构是怎样发的呢?
 
發結構是不能定議流的,可以用array of char,註意長度。
 
改成array of char了,编译通过,但运行出错了,帮看是下是哪里出了问题?
type TTQQUDPHead=record
MsgType:String[2];
end;
type TBody=packed record
ss:array [0..81920] of Char;
end;
type
TSdate=record
Head:TTQQUDPHead;
Body:TBody;
end;
发送
var
jpg: TJpegImage;
memStream: TMemoryStream;
sd:TSdate;
dx:integer;
CharMemoryStream:pchar;
begin
sd.Head.MsgType:='1';
memStream := TMemoryStream.Create;
jpg := TJpegImage.Create;
jpg.Assign(Image.Picture.Bitmap);
jpg.CompressionQuality := 2;
jpg.JPEGNeeded;
jpg.Compress;
jpg.SaveToStream(memStream);
jpg.Free;
CopyMemory(CharMemoryStream,memStream.Memory,memStream.Size); //运行到这出错
StrPCopy(sd.body.ss,CharMemoryStream);
try
IdUDPServer1.SendBuffer('192.168.1.88',8581,sd,SizeOf(sd));
except
end;
memStream.Free;
finally
Image.Canvas.Unlock;
end;

接收
var
memStream: TMemoryStream;
jpg: TJpegImage;
BufferLen: Integer;
_Head:TTQQUDPHead;
rr:TBody;
CharMemoryStream:pchar;
begin
memStream := TMemoryStream.Create;
BufferLen := AData.Size;
try
AData.Read(_Head,Sizeof(_Head));
AData.Read(rr,Sizeof(rr));
except
end;
jpg := TJpegImage.Create;
CharMemoryStream:= rr.ss;
CopyMemory(memStream.Memory,CharMemoryStream,SizeOf(CharMemoryStream));
jpg.LoadFromStream(memStream);
Image.Picture.Bitmap.Assign(jpg);
jpg.Free;
memStream.Free;
 
如果图片较大的话应该分成若干个小包发送,每个小包前带上序号标示,接受方按顺序组织
当然最好是用tcp
 
to迷糊
包前带上序号标示也是我准备想实现的功能,有相关的代码吗,贴点出来给小弟参考一下啊,谢谢
 
8好意思,没有代码
 
寫到ss中不需要通過Pchar變量來轉。
可以直接用memStream.Read(ss,sizeof(ss));註意內存流Size是否超過ss的長度,如果超過,要分多次發送。內存流讀一次會減少相應的部分。序號也是接照內存流的Size與ss的
長度計算出來的。
接收同樣可以定義一個array of char變量,註意接收的內存流變量應為全局變量,用它的
write方法來寫入。如memstream.write(ss,sizeof(ss));
 
发送改为memStream.Read(sd.body.ss,memStream.Size);了
接收改为
_Body:TBody;
begin
AData.Read(_Head,Sizeof(_Head));
AData.Read(_Body,Sizeof(_Body));
memStream := TMemoryStream.Create;
memStream.Write(_Body.ss,sizeof(_Body.ss));
jpg := TJpegImage.Create;
jpg.LoadFromStream(memStream);
Image.Picture.Bitmap.Assign(jpg); //在这里出错了
jpg.Free;
memStream.Free;
现在的情况是发送和接收的数据都是对的了,就是在显示图象Image.Picture.Bitmap.Assign(jpg); 出错,请再帮忙想想是什么原因造成的,非常感谢前面几位兄弟的帮助
 
在 jpg.LoadFromStream(memStream);前加上
memStream.Position := 0;
 
正如leadyli所说ok了,非常感谢
 
后退
顶部