UDPServer Assign Event Handler Not Working

UDPServer Assign Event Handler Not Working

Post by Adria » Thu, 13 Mar 2008 11:28:02



Hello all,

I am trying to make a DLL that simply broadcasts a message out
using UDP and through a custom port. I assign a custom function
to the event OnUDPRead but it never gets triggered, and I am not
sure why not...

--------------------------------------------------------

type TDummyClass = class
public
procedure UDPServerRead(Sender: TObject; AData: TBytes; ABinding: TIdSocketHandle);
end;

var
UDPServer: TidUDPServer;
DummyClass: TDummyClass;

procedure TDummyClass.UDPServerRead(Sender: TObject; AData: TBytes; ABinding: TIdSocketHandle);
begin
Showmessage('Got here!!');
end;

procedure InitializeDLL; cdecl; export;
begin
DummyClass := TDummyClass.Create;

UDPServer := TidUDPServer.Create(nil);
UDPServer.ThreadedEvent := True;
UDPServer.BroadcastEnabled := True;
UDPServer.Active := True;
UDPServer.OnUDPRead := DummyClass.UDPServerRead;
UDPServer.DefaultPort := 33;
end;

procedure Broadcast(MessageToBroadcast: ShortString; Port: Integer); cdecl; export;
begin

I know broadcasting works, as I have a seperate application to
show them when they are received - its just when the UPDServer
inside the DLL receives broadcasts it does nothing (its OnUPDRead
event is not activated

UDPServer.Broadcast(xxx, aaa);
end;

Why does this not work? The DLL sends broadcasts successfully (I
have removed the broadcasting code for clarity), but it does not
fire the OnUDPRead event when it receives broadcasts?

Thanks,
Adrian ;)
 
 
 

UDPServer Assign Event Handler Not Working

Post by Team » Thu, 13 Mar 2008 13:09:00


Because you are listening for data on the wrong Port.


You should be setting that before setting the Active property, not
afterwards. Otherwise, you risk missing data that could arrive right away.


You must set that value before setting the Active property, not afterwards.
Setting it has no effect after the server is already activated. Since you
are not setting it beforehand, the Port used is still 0 when the server is
activated, and so it ends up listening on a random port instead of the one
you wanted.


Gambit

 
 
 

UDPServer Assign Event Handler Not Working

Post by Adria » Thu, 13 Mar 2008 17:40:41


Awesome, thank you so much - that fixed the problem ;)

You would not believe how much that helped me ;)

Cheers,
Adrian ^^