indy tcpclient发送和接收
下面的代码是实现从客户端发送一个命令给服务端,
服务端接收到命令后截取屏幕并发送给客户端,客户端接收保存显示 客户端: var ftmpStream : TFileStream; bitmap:Tbitmap; begin try with IdTCPClient do begin if connected then DisConnect; Host := edtServerHost.text; Port := StrToInt(edtServerPort.text); Connect; WriteLn('SRN'); // delete if exists // in production situation you might store binary downloads like this in a cache folder if FileExists(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp') then DeleteFile(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp'); ftmpStream := TFileStream.Create(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp',fmCreate); // bmpstream.Clear; // bitmap:=Tbitmap.Create; while connected do // ReadStream(bmpStream,-1,true); ReadStream(fTmpStream,-1,true); FreeAndNil(fTmpStream); Disconnect; imgMain.Picture.LoadFromFile(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp'); // bitmap.LoadFromStream(bmpStream); // imgMain.Picture.Bitmap:=bitmap; end; except on E : Exception do ShowMessage(E.Message); end; 服务端: procedure TFMain.IdTCPServerExecute(AThread: TIdPeerThread); var s, sCommand, sAction : string; fStream : TFileStream; tBM : tbitmap; begin CS.Enter; try s := uppercase(AThread.Connection.ReadLn); sCommand := copy(s,1,3); sAction := copy(s,5,100); if sCommand = 'SRN' then begin // in production version you would use a unique file name such as one generated // from a tickcount plus clint IP / id etc. // take snapshot GetBitMap(); // copy file stream to write stream AThread.Connection.OpenWriteBuffer; AThread.Connection.WriteStream(bmpStream); AThread.Connection.CloseWriteBuffer; // free the file stream bmpstream.Clear; AThread.Connection.Disconnect; End else if (sCommand <> 'LST') and (sCommand <> 'PIC') and (sCommand <> 'SRN') then Begin AThread.Connection.WriteLn('ERR : Unknown command / action'); AThread.Connection.Disconnect; end; except on E : Exception do ShowMessage(E.Message); End; CS.Leave; end; |