如何使用TIdTCPServer连续发送消息?

时间:2015-02-18 10:23:25

标签: delphi indy

我需要创建一个delphi应用程序,当它启动时,服务器也会启动,并立即开始发送消息,但我还没有找到示例或教程,近5000页的Indy手册并不清楚我怎么能这样做......

2 个答案:

答案 0 :(得分:3)

Server push example application form

此示例使用带有主窗体的Delphi 2009 VCL应用程序,该窗体仅包含一个可视组件,名为“MemoLog”的TMemo。

客户端和服务器都在FormCreate事件中启动。请注意,客户端代码不处理连接丢失,但这可以通过线程内的单独重新连接循环来实现。

procedure TServerPushExampleForm.FormCreate(Sender: TObject);
begin
  ExampleServer := TMyPushServer.Create;
  ExampleServer.DefaultPort := 8088;
  ExampleServer.Active := True;

  ExampleClient := TMyPushClientThread.Create('localhost', 8088,
    MemoLog.Lines);
end;

服务器

服务器代码使用TIdTCPCustomServer子类,该子类等待随机时间,然后将字符串发送到客户端。

function TMyPushServer.DoExecute(AContext: TIdContext): Boolean;
begin
  Result := inherited;

  // simulate hard work
  Sleep(Random(3000));

  AContext.Connection.IOHandler.WriteLn(
    'Completed at ' + TimeToStr(Now), IndyTextEncoding_UTF8);
end;

客户端

客户端代码使用TThread子类异步运行,而不会阻塞主VCL线程。它包含一个私有TIdTCPClient实例,并定期尝试从连接接收字符串。

...
  S := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
...

完整德尔福表格代码

以下是示例主窗体的完整代码。

unit Unit1;

interface

uses
  IdCustomTCPServer, IdTCPClient, IdContext,
  SysUtils, Classes, Forms, StdCtrls, Controls;

type
  TMyPushClientThread = class(TThread)
  private
    TCPClient: TIdTCPClient;
    FLog: TStrings;
  public
    constructor Create(AHost: string; APort: Word; ALog: TStrings);
    destructor Destroy; override;
    procedure Execute; override;
  end;

  TMyPushServer = class (TIdCustomTCPServer)
  protected
    function DoExecute(AContext: TIdContext): Boolean; override;
  end;

  TServerPushExampleForm = class(TForm)
    MemoLog: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    ExampleClient: TMyPushClientThread;
    ExampleServer: TMyPushServer;
  end;

var
  ServerPushExampleForm: TServerPushExampleForm;

implementation

uses
  IdGlobal;

{$R *.dfm}

procedure TServerPushExampleForm.FormCreate(Sender: TObject);
begin
  ExampleServer := TMyPushServer.Create;
  ExampleServer.DefaultPort := 8088;
  ExampleServer.Active := True;

  ExampleClient := TMyPushClientThread.Create('localhost', 8088, MemoLog.Lines);
end;

procedure TServerPushExampleForm.FormDestroy(Sender: TObject);
begin
  ExampleServer.Free;
  ExampleClient.Terminate;
  ExampleClient.WaitFor;
  ExampleClient.Free;
end;

{ TMyPushServer }

function TMyPushServer.DoExecute(AContext: TIdContext): Boolean;
begin
  Result := inherited;

  // simulate hard work
  Sleep(Random(3000));

  AContext.Connection.IOHandler.WriteLn(
    'Completed at ' + TimeToStr(Now), IndyTextEncoding_UTF8);
end;

{ TMyPushClientThread }

constructor TMyPushClientThread.Create(AHost: string; APort: Word; ALog: TStrings);
begin
  inherited Create(False);

  FLog := ALog;

  TCPClient := TIdTCPClient.Create;
  TCPClient.Host := AHost;
  TCPClient.Port := APort;
  TCPClient.ReadTimeout := 500;
end;

destructor TMyPushClientThread.Destroy;
begin
  TCPClient.Free;
  inherited;
end;

procedure TMyPushClientThread.Execute;
var
  S: string;
begin
  TCPClient.Connect;

  while not Terminated do
  begin
    S := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);

    if not TCPClient.IOHandler.ReadLnTimedout then
    begin
      TThread.Queue(nil,
        procedure
        begin
          FLog.Append(S);
        end);
    end;

  end;

  TCPClient.Disconnect;
end;

end.

(来自https://mikejustin.wordpress.com/2014/04/19/indy-10-tidtcpserver-server-side-message-push-example/

答案 1 :(得分:2)

Indy的工作方式是让客户端(TidTCPClient)连接到服务器(TidTCPServer),然后在它们之间来回交换数据,直到连接被故意或过早断开连接。

我只是指这里的实际Indy TCP组件,而不是你看到你的应用程序的方式。

在应用程序级别,您可以将应用程序视为服务器应用程序,将另一个应用程序视为客户端应用程序,但两者都可以/可能包含与其他应用程序通信的TidTCPClient和TidTCPServer组件。这意味着服务器应用程序可以通过服务器应用程序的TidTCPClient组件启动与客户端应用程序的连接,客户端应用程序将通过其TidTCPServer组件接收连接。这将是一种可能的解决方案,但请记住,通常客户端是动态的,而且不断变化,而服务器通常是静态的,因此跟踪客户端的位置将是一项任务。太多的头痛和太多的工作。

所以我认为最好让客户跟踪他们很少变化的服务器,因此最好为服务器应用程序安装一个TidTCPServer组件,让它在开始发送消息之前等待客户端连接。

所以要实施;您的客户必须不断尝试定期连接到服务器,直到找到服务器。然后,服务器可以发送任意数量的消息,直到被要求停止或直到过早断开,在这种情况下,循环将重新启动。 Indy有一些方法可以跟踪客户端连接,您可以通过这些方式保留客户端的内部列表。这更有意义。这是大多数客户端 - 服务器应用程序的工作方式。想想Skype和任何Web服务器。客户联系服务器并在需要时接收数据。

在服务器端:

  • 创建TidTCPServer对象。
  • 设置TidTCPServer以侦听其一个或多个本地IP 地址并为它们选择IP端口。
  • 将代码分配给TidTCPServer,它将在客户端运行时立即运行 通过TidTCPServer的OnExecute连接到它。在此代码中,您将把消息发送到连接的客户端。
  • 激活TidTCPServer以使其处于聆听模式。

在客户端:

  • 创建TidTCPClient对象。
  • 设置TidTCPClient以使用特定的主机和端口(IP 服务器的地址/主机名和您选择的端口
  • 在具有间隔的重复循环中尝试连接到服务器。
  • 一旦建立连接,客户端就可以发送 服务器某事或立即尝试从连接中读取 这是服务器发送内容时会收到的内容

这种类型的操作有很多例子。你必须先尝试,如果你挣扎,你总能提出具体问题的问题。