以下是部分delphi 代码:
以下Server 端处理.:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
RecvTxt :string;
begin
RecvTxt := Socket .ReceiveText;
if RecvTxt = 'Get Screen' then
begin
GetScreen (Image );
ImageStream .Clear ;
Image .SaveToStream (ImageStream );
ImageStream .Position := 0;
Socket .SendText ('SIZE:'+IntToStr (ImageStream .Size ));
end
else
if RecvTxt = 'Ok' then
begin
ImageStream .Position := 0;
Socket .SendStream (ImageStream );
end
else
begin
Memo1 .Lines .Add ('Time : '+ FormatDateTime ('hh:nn:ss',now()) + 'Recv: ' + RecvTxt + ' From: ' + Socket .LocalAddress );
end;
end;
以下Client 端处理.:
procedure TForm2.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
Var
RecvText :String ;
Buf :array[0..10000] of Byte ;
RecvLen:Integer ;
Bmp :TBitmap;
begin
if ImageSize = 0 then //Imagesize 为单员级变量.
begin
RecvText := Socket .ReceiveText();
if copy(RecvText ,1,4) = 'SIZE' then
begin
ImageSize := StrToInt (Copy(RecvText ,6,Length(RecvText)-5 ));
ClientSocket1 .Socket .SendText ('Ok');
end;
end
else
begin
RecvLen := Socket .ReceiveLength ;
StatusBar1 .SimpleText := '正在接收数据,数据长度为'+ IntToStr (RecvLen )+ 'Byte.';
Socket .ReceiveBuf (Buf ,RecvLen );
if MemoryStream .Size < ImageSize then
begin
MemoryStream.Write(Buf ,RecvLen );
end;
if MemoryStream .Size >= ImageSize then
begin
MemoryStream .Position := 0;
Jpg .LoadFromStream (MemoryStream );
try
Jpg .SaveToFile ('c:/windows/desktop/1.jpg');
finally
ImageSize := 0;
end;
end;
end;
end;