如何使用TIdTCPServer连续发送消息? [英] How to continuously send messages with TIdTCPServer?
问题描述
我需要创建一个delphi应用程序,在该应用程序启动时,服务器也将启动,并立即开始发送消息,但是我还没有找到示例或教程,并且Indy手册将近5000页并不清楚我该怎么做...
I need to create a delphi application where when it's started the server is started as well and starts sending messages immediately, but I haven't found an example or tutorial and the nearly 5000 page Indy manual doesn't make it clear to me how I can do this...
推荐答案
此示例使用带有主要表单的Delphi 2009 VCL应用程序,该表单仅包含一个可视组件,一个名为 MemoLog的TMemo。
This example uses a Delphi 2009 VCL application with a main form, which contains only one visual component, a TMemo named "MemoLog".
客户端和服务器都在FormCreate事件中启动。请注意,客户端代码不会处理连接丢失,但是可以在线程内使用单独的重新连接循环来实现。
Client and server are both started in the FormCreate event. Note that the client code does not handle connection loss, but this can be implemented with a separate re-connect loop within the thread.
procedure TServerPushExampleForm.FormCreate(Sender: TObject);
begin
ExampleServer := TMyPushServer.Create;
ExampleServer.DefaultPort := 8088;
ExampleServer.Active := True;
ExampleClient := TMyPushClientThread.Create('localhost', 8088,
MemoLog.Lines);
end;
服务器
服务器代码使用TIdTCPCustomServer子类,它等待一个随机时间,然后将字符串发送到客户端。
Server
The server code uses a TIdTCPCustomServer subclass which waits for a random time and then sends a string to the client.
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实例,并定期尝试从连接中接收字符串。
Client
The client code uses a TThread subclass to run asynchronously without blocking the main VCL thread. It contains a private TIdTCPClient instance, and periodically tries to receive a string from the connection.
...
S := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
...
完整的Delphi表单代码
下面是示例主表单的完整代码。
Full Delphi Form Code
Below is the full code for the example main form.
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 / )
这篇关于如何使用TIdTCPServer连续发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!