在TIdCmdTCPServer的基本使用方面需要帮助 [英] need help with basic use of TIdCmdTCPServer

查看:66
本文介绍了在TIdCmdTCPServer的基本使用方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用delphi XE中的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?

谢谢!mp

推荐答案

您不能将 TIdCmdTCPClient TIdCmdTCPServer 一起使用. TIdCmdTCPClient 运行一个内部线程,该线程不断从连接中读取数据,但是 SendCmd()方法也执行其自身的读取操作,因此两者相互干扰并相互抢夺他人的数据.这就是为什么您会收到超时错误的原因.您需要更改客户端代码以使用 TIdTCPClient 而不是 TIdCmdTCPClient .

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.

这篇关于在TIdCmdTCPServer的基本使用方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆