W
wzwcn
Unregistered / Unconfirmed
GUEST, unregistred user!
我用INDY的IdUDPServer和IdUDPClient发送图片。在客户端打开一张JPEG图,然后发给服务端,服务端接收并显示出来。我现在的问题是,服务端接到后显示不出来,好象格式不对,代码如下,请高手指点,谢谢:
客户端发送代码:
procedure TForm1.BitBtn2Click(Sender: TObject);
var mem:Tmemorystream;
buf:array[0..1023] of char;
len,posi:integer;
RecStr:string;
jpeg:Tjpegimage;
begin
mem:=Tmemorystream.Create;
jpeg:=Tjpegimage.Create;
jpeg.LoadFromFile(OpenDialog1.FileName);
jpeg.SaveToStream(mem);
//image1.Picture.Graphic.SaveToStream(mem);
posi:=0;
IdUDPClient1.Host:=edit1.Text;
IdUDPClient1.Port:=strtoint(edit2.text);
IdUDPClient1.Active:=true;
IdUDPClient1.Send('start');
RecStr:=IdUDPClient1.ReceiveString();
if RecStr='ok' then
begin
While Posi<Mem.Size do
begin
len:=1024;
if Mem.Size-Posi<1024 then
Len:=Mem.Size-Posi;
Mem.Read(buf,len);
IdUDPClient1.SendBuffer(buf,Len);
inc(posi,len);
RecStr:=IdUDPClient1.ReceiveString();
if RecSTr<>'ok' then
break;
Application.ProcessMessages;
end; //end while
IdUDPClient1.Send('end');
end;
mem.Free;
jpeg.Free;
end;
端务端接收代码:
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var Str:string;
jpeg:Tjpegimage;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Str='start' then
begin
mem:=Tmemorystream.Create;
str:='ok';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end
else if Str='end' then
begin
jpeg:=Tjpegimage.Create;
jpeg.LoadFromStream(mem);
//showmessage(inttostr(mem.Size));
//jpeg.SaveToFile('e:/okok.jpg');
Image1.Picture.Assign(jpeg);
jpeg.Free;
mem.Free;
end
else
begin
Mem.Seek(0,2);
AData.Seek(0,0);
Mem.CopyFrom(AData,AData.Size);
str:='ok';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;
客户端发送代码:
procedure TForm1.BitBtn2Click(Sender: TObject);
var mem:Tmemorystream;
buf:array[0..1023] of char;
len,posi:integer;
RecStr:string;
jpeg:Tjpegimage;
begin
mem:=Tmemorystream.Create;
jpeg:=Tjpegimage.Create;
jpeg.LoadFromFile(OpenDialog1.FileName);
jpeg.SaveToStream(mem);
//image1.Picture.Graphic.SaveToStream(mem);
posi:=0;
IdUDPClient1.Host:=edit1.Text;
IdUDPClient1.Port:=strtoint(edit2.text);
IdUDPClient1.Active:=true;
IdUDPClient1.Send('start');
RecStr:=IdUDPClient1.ReceiveString();
if RecStr='ok' then
begin
While Posi<Mem.Size do
begin
len:=1024;
if Mem.Size-Posi<1024 then
Len:=Mem.Size-Posi;
Mem.Read(buf,len);
IdUDPClient1.SendBuffer(buf,Len);
inc(posi,len);
RecStr:=IdUDPClient1.ReceiveString();
if RecSTr<>'ok' then
break;
Application.ProcessMessages;
end; //end while
IdUDPClient1.Send('end');
end;
mem.Free;
jpeg.Free;
end;
端务端接收代码:
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var Str:string;
jpeg:Tjpegimage;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Str='start' then
begin
mem:=Tmemorystream.Create;
str:='ok';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end
else if Str='end' then
begin
jpeg:=Tjpegimage.Create;
jpeg.LoadFromStream(mem);
//showmessage(inttostr(mem.Size));
//jpeg.SaveToFile('e:/okok.jpg');
Image1.Picture.Assign(jpeg);
jpeg.Free;
mem.Free;
end
else
begin
Mem.Seek(0,2);
AData.Seek(0,0);
Mem.CopyFrom(AData,AData.Size);
str:='ok';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;