gpt4 book ai didi

delphi - 控制台应用程序中的 TIdTCPServer.OnExecute

转载 作者:行者123 更新时间:2023-12-01 17:58:34 28 4
gpt4 key购买 nike

我需要编写一个 Delphi 2009 应用程序,它从套接字读取数据。为此,我需要为 TIdTCPServer.OnExecute 事件编写一个事件处理程序。

我发现了很多在 GUI 应用程序中实现此功能的示例,但我需要在控制台应用程序(没有任何窗口)中执行此操作。

我应该如何修改下面的代码以添加一个事件处理程序(将其附加到 TCPServer),该事件处理程序将收到的每条消息打印到调试输出中?

unit ReceivingThreadUnit;

interface

uses
Classes,
IdTCPServer,
IdSocketHandle,

SysUtils,
Windows;

type
ReceivingThread = class(TThread)
private
TCPServer: TIdTCPServer;
public
procedure Run();

end;

implementation

procedure ReceivingThread.Run();
var
Bindings: TIdSocketHandles;
begin
TCPServer := TIdTCPServer.Create(nil);

//setup and start TCPServer
Bindings := TIdSocketHandles.Create(TCPServer);
try
with Bindings.Add do
begin
IP := '127.0.0.1';
Port := 9998;
end;
try
TCPServer.Bindings:=Bindings;
// Here I want to attach TCPServer to an OnExecute event handler
TCPServer.Active:=True;
except on E:Exception do
OutputDebugString(PChar(E.ToString));
end;
finally
Bindings.Free;
TCPServer.Free;
end;
TCPServer.Active := true;
end;

end.

最佳答案

正如 David 所说(但没有完全展示),您需要在线程类中声明一个方法,然后将其分配给 OnExecute 事件。

顺便说一句,您不应该手动创建 TIdSocketHandles 集合。改为在现有 TIdTCPServer.Bindings 集合上调用 Add()

试试这个:

unit ReceivingThreadUnit;

interface

uses
Classes,
IdTCPServer,
IdSocketHandle,
IdContext,

SysUtils,
Windows;

type
ReceivingThread = class(TThread)
private
TCPServer: TIdTCPServer;
procedure ExecuteHandler(AContext: TIdContext);
public
procedure Run;
end;

implementation

procedure ReceivingThread.ExecuteHandler(AContext: TIdContext);
begin
//...
end;

procedure ReceivingThread.Run;
begin
//setup and start TCPServer
TCPServer := TIdTCPServer.Create(nil);
try
with TCPServer.Bindings.Add do
begin
IP := '127.0.0.1';
Port := 9998;
end;
TCPServer.OnExecute := ExecuteHandler;
try
TCPServer.Active := True;
except
on E: Exception do
OutputDebugString(PChar(E.ToString));
end;
while not Terminated do
Sleep(1000);
TCPServer.Active := False;
finally
TCPServer.Free;
end;
end;

end.

话虽如此,您的接收线程实际上非常多余,因为 TIdTCPServer 已经是多线程的,因此您也可以完全消除线程类:

program MyApp;

{$APPTYPE CONSOLE}

{$R *.res}

uses
Classes,
IdTCPServer,
IdSocketHandle,
IdContext,

SysUtils,
Windows;

type
TCPServerEvents = class
public
class procedure ExecuteHandler(AContext: TIdContext);
end;

class procedure TCPServerEvents.ExecuteHandler(AContext: TIdContext);
begin
//...
end;

var
TCPServer: TIdTCPServer;
begin
//setup and start TCPServer
TCPServer := TIdTCPServer.Create(nil);
try
with TCPServer.Bindings.Add do
begin
IP := '127.0.0.1';
Port := 9998;
end;

TCPServer.OnExecute := TCPServerEvents.ExecuteHandler;

try
TCPServer.Active := True;
except
on E: Exception do
OutputDebugString(PChar(E.ToString));
end;

while (not stop condition) do
Sleep(1000);

TCPServer.Active := False;
finally
TCPServer.Free;
end;
end.

关于delphi - 控制台应用程序中的 TIdTCPServer.OnExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13357329/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com