2012-11-13 137 views
1

我需要编写一个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. 

回答

1

您需要为您的类添加事件处理程序。然后将其连接起来:

TCPServer.OnExecute := Self.ExecuteHandler; 
4

正如大卫说(但并未完全显示),你需要在你的线程类来声明一个方法,然后将其分配给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. 

虽这么说,你receiveing线程实际上是非常多余的,因为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. 
+0

尼斯。我发现你的技巧有点肮脏。但我明白你为什么不想实例化一个对象。一个很好的变体是使用类方法。当然你需要声明一个类,但是你不需要实例化一个你想要避免的锅炉主板的实例。 –

+0

很高兴知道,谢谢。我更新了我的答案以使用它。 –