indy tcpclient发送和接收
下面的代码是实现从客户端发送一个命令给服务端,服务端接收到命令后截取屏幕并发送给客户端,客户端接收保存显示客户端:varftmpStream : TFileStream;bitmap:Tbitmap;begintrywith IdTCPClient dobeginif 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 folderif FileExists(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp') thenDeleteFile(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;excepton E : Exception doShowMessage(E.Message);end;服务端:procedure TFMain.IdTCPServerExecute(AThread: TIdPeerThread);vars, sCommand, sAction : string;fStream : TFileStream;tBM : tbitmap;beginCS.Enter;trys := uppercase(AThread.Connection.ReadLn);sCommand := copy(s,1,3);sAction := copy(s,5,100);if sCommand = 'SRN' thenbegin// in production version you would use a unique file name such as one generated// from a tickcount plus clint IP / id etc.// take snapshotGetBitMap();// copy file stream to write streamAThread.Connection.OpenWriteBuffer;AThread.Connection.WriteStream(bmpStream);AThread.Connection.CloseWriteBuffer;// free the file streambmpstream.Clear;AThread.Connection.Disconnect;Endelseif (sCommand <> 'LST') and (sCommand <> 'PIC') and (sCommand <> 'SRN') thenBeginAThread.Connection.WriteLn('ERR : Unknown command / action');AThread.Connection.Disconnect;end;excepton E : Exception doShowMessage(E.Message);End;CS.Leave;end;