【发布时间】:2011-09-07 01:45:06
【问题描述】:
我正在开发一个在 delphi XE 中使用 TIdCmdTCPServer 的概念验证应用程序。
我的代码似乎有问题,因为只有第一个命令有效。如果我重复相同的命令,它会“超时”。请参阅下面的客户端代码列表。
这是我的命令处理程序:
procedure TForm1.IdCmdTCPServer1CommandHandlersGetDateTimeCommand(ASender: TIdCommand);
begin
ASender.Reply.SetReply(200, 'OK!');
ASender.Reply.Text.Add(DateTimeToStr(Now));
ASender.SendReply; // I expect this must be redundant
end;
这是服务器组件(这里没什么特别的;我设置了端口号并创建了一个命令处理程序):
object IdCmdTCPServer1: TIdCmdTCPServer
Bindings = <>
DefaultPort = 7000
CommandHandlers = <
item
CmdDelimiter = ' '
Command = 'GetDateTime'
Disconnect = False
Name = 'TIdCommandHandler0'
NormalReply.Code = '200'
ParamDelimiter = ' '
ParseParams = True
Tag = 0
OnCommand = IdCmdTCPServer1CommandHandlersGetDateTimeCommand
end
ExceptionReply.Code = '500'
ExceptionReply.Text.Strings = (
'Unknown Internal Error')
Greeting.Code = '200'
Greeting.Text.Strings = (
'Welcome')
HelpReply.Code = '100'
HelpReply.Text.Strings = (
'Help follows')
MaxConnectionReply.Code = '300'
MaxConnectionReply.Text.Strings = (
'Too many connections. Try again later.')
ReplyTexts = <>
ReplyUnknownCommand.Code = '400'
ReplyUnknownCommand.Text.Strings = (
'Unknown Command')
Left = 64
Top = 8
end
这是出现问题的客户端代码:
Client.Connect;
try
// retrieve welcome text
memo1.lines.AddStrings(Client.LastCmdResult.Text);
Client.SendCmd('GetDateTime', 200);
memo1.lines.AddStrings(Client.LastCmdResult.Text);
//////////////////////////// FAILS HERE (timeout)
Client.SendCmd('GetDateTime', 200);
memo1.lines.AddStrings(Client.LastCmdResult.Text);
finally
Client.Disconnect(true);
end;
和客户端组件(这里没什么特别的;我设置了主机和端口号):
object Client: TIdCmdTCPClient
ConnectTimeout = 1000
Host = '127.0.0.1'
IPVersion = Id_IPv4
Port = 7000
ReadTimeout = 1000
CommandHandlers = <>
ExceptionReply.Code = '500'
ExceptionReply.Text.Strings = (
'Unknown Internal Error')
Left = 144
Top = 96
end
任何想法为什么会发生这种情况?
谢谢! mp
【问题讨论】: