我需要编写一个Delphi 2009应用程序,它从套接字读取数据。为此,我需要为TIdTCPServer.OnExecute
事件编写事件处理程序。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.
尼斯。我发现你的技巧有点肮脏。但我明白你为什么不想实例化一个对象。一个很好的变体是使用类方法。当然你需要声明一个类,但是你不需要实例化一个你想要避免的锅炉主板的实例。 –
很高兴知道,谢谢。我更新了我的答案以使用它。 –