I need to write a Delphi 2009 application, which reads data from a socket. To do this, I need to write an event handler for the TIdTCPServer.OnExecute
event.
I found lots of examples for implementing this in GUI applications, but I need to do it in a console application (without any windows).
How should I modify the code below in order to add an event handler (attach it to TCPServer
), which prints every received message into the debug output?
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.