写一个线程,客户端一点连接到服务器后就创建并运行该线程, 线程里面: while ((not Terminated) and dm.tcpclient.Connected do begin ires := DM.tcpLog.ReadFromStack; if ires > 0 then begin //此处处理 end; end;
下面的代码是实现从客户端发送一个命令给服务端, 服务端接收到命令后截取屏幕并发送给客户端,客户端接收保存显示 客户端: 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;
第二方法:采用多线程来读取IndyTcpClient。
线程里面:
while ((not Terminated) and dm.tcpclient.Connected do
begin
ires := DM.tcpLog.ReadFromStack;
if ires > 0 then
begin
//此处处理
end;
end;
服务器端OnExcuete怎么读取,客户端的和服务器端的差不多,不过就是没有单独的事件而已,,如果你对多线程不了解,可以先用timer控件来写,再试着用多线程。方法就是这样,再不行,到网上搜索indy的例子,很多的。
多线程也不一定好,得看具体情况.
1.推荐用阻断器吧,比如TIdLogEvent
2.自己定义数据包,用 IdTCPClient1.ReadBuffer;,参考indydemo
服务端接收到命令后截取屏幕并发送给客户端,客户端接收保存显示
客户端:
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;