我刚刚成功让客户端(IdTCPClient)按照要求向服务器(IdTCPServer)发送了一条消息。但是,如何让客户端等待响应或适当超时呢?
谢谢, Adrian
procedure TForm1.ReadTimerElapsed(Sender: TObject);
var
S: String;
begin
...
// connect
IdTCPClient1.Connect;
// send data
...
// use one of the Read methods to read the response.
// some methods have a timeout parameter,
// and others set a timeout flag
S := IdTCPClient1.IOHandler.ReadLn(...);
if IdTCPClient1.IOHandler.ReadLnTimedOut then
...
else
...
end;
procedure TForm1.SendCmdButtonClick(Sender: TObject);
var
Resp: String;
begin
Client.IOHandler.WriteLn('CMD');
Resp := Client.IOHandler.ReadLn;
end;
服务器:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
Cmd: String;
begin
Cmd := AContext.Connection.IOHandler.ReadLn;
...
AContext.Connection.IOHandler.WriteLn(...);
end;
或者,您可以使用TIdTCPConnection.SendCmd()
方法:
客户端:
procedure TForm1.SendCmdButtonClick(Sender: TObject);
begin
// any non-200 reply will raise an EIdReplyRFCError exception
Client.SendCmd('CMD', 200);
// Client.LastCmdResult.Text will contain the response text
end;
服务器:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
Cmd: String;
begin
Cmd := AContext.Connection.IOHandler.ReadLn;
...
if (Command is Successful) then
AContext.Connection.IOHandler.WriteLn('200 ' + ...);
else
AContext.Connection.IOHandler.WriteLn('500 Some Error Text here');
end;
TIdCmdTCPServer
,您可以使用TIdCmdTCPServer.CommandHandlers
集合在设计时定义您的命令,并为每个命令分配OnCommand
事件处理程序,而不是使用OnExecute
事件手动读取和解析命令,例如:// OnCommand event handler for 'CMD' TIdCommandHandler object...
procedure TForm1.IdCmdTCPServer1CMDCommand(ASender: TIdCommand);
begin
...
if (Command is Successful) then
ASender.Reply.SetReply(200, ...);
else
ASender.Reply.SetReply(500, 'Some Error Text here');
end;
我已经有一段时间没有使用Indy组件了(或者说Delphi),但我认为TIdTCPClient不是异步的,因此没有可以设置的OnData或类似的事件。
您需要从父类(TIdTCPConnection)调用其中一个读取方法,例如ReadLn(...)。或者,您可以考虑使用许多TIdTCPClient后代的Indy组件之一。
该类的文档可以在这里找到。
WriteLn()
发送一个以 CRLF 为分隔符的字符串。TIdIOHandler.ReadLn()
方法读取一个带有分隔符的字符串,其中LF
是默认的分隔符(当分隔符是LF
时,ReaDLn() 在内部考虑了CRLF
)。 - Remy Lebeau