开发者

need help with basic use of TIdCmdTCPServer

I'm working on a proof-of-concept app using TIdCmdTCPServer in delphi XE.

There seems to be something wrong with my code because only the first command works. If i repeat the same command, it "times out". See client code listing farther down below.

here's my command handler:

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;

Here's the server component (nothing special here; I set the port # and created a command handler):

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

here's the client code where the problem occurs:

  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;

and the client component (nothing special here; i set the host & port #):

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

Any ideas why is this happening?

Thank you! mp


You cannot use a TIdCmdTCPClient with a TIdCmdTCPServer. TIdCmdTCPClient runs an internal thread that continuously reads from the connection, but the SendCmd() method also performs read operations of its own, so the two interfer with each other and grab each other's data. That is why you get timeout errors. You need to change your client code to use a TIdTCPClient instead of a TIdCmdTCPClient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜