B4J Question UDP PacketArrived event in a class

woniol

Active Member
Licensed User
Hello,
I have a question regarding UDP_PacketArrived.

I use Udpsocket in my class, where
UDP_PacketArrived sub is declared.

Then I have two objects of this class declared in my main program.

Now, how to know in which of them the
UDP_PacketArrived event will be raised?
Can I declare or assign it somehow?

Hope the question is clear...and someone can help with that.
Thanks in advance.
Woniol

PS. Please move it to B4J questions section
 

woniol

Active Member
Licensed User
I already have a string variable, which is the ip adress for the packet to be sent, and the event is is raised always in the same instance, but initialize and packet sent in both.

Might that be an issue with UDP class?
 
Last edited:

DonManfred

Expert
Licensed User
What is so complicated?
The events are raised in the instance where the UDP is initialized.
Create one Class. make it a processglobal public variable.

You then can use the classmethods from any forum using main.youclassvariable.dothis. The events are raised in the class for sure. you can raise an event from the class to the calling form...
 

woniol

Active Member
Licensed User
It is not complicated, I already tried that and in my case the event was always raised in one instance, not always the correct one.
I use it in nonUI app.
The code I use I quite complicated and I also use it to communicate with the specific device (Broadlink), so it is not possible to test by someone just by posting my code.
That's why I asked for some workings example.

My working solution is to pass the event to the main module and then pass it back to the correct instance identified by the Ip address form the incoming packet.
Just wanted to know if it is a normal behavior or maybe some issue....
 

woniol

Active Member
Licensed User
Here is the example.
The log output I get is the following:
B4X:
Init d
Auth d
Init d2
Auth d2
Udpsock_PacketArrived instance:192.168.0.20 from host:192.168.0.20
Udpsock_PacketArrived instance:192.168.0.20 from host:192.168.0.21
I would expect the same instance (ip) and from host.
It will be difficult to reproduce without the real devices, but maybe I do something wrong in the code itself.
 

Attachments

OliverA

Expert
Licensed User
You will need to give each class instance its own port number. There is no correlation between sending a UDP packet and getting a return answer except for the port number used on the server's sending/receiving side. Looks like whichever class calls the UDPSocket Initialize/Initialize2 method with a given port number will receive the messages sent to/arriving at that port number.
 

woniol

Active Member
Licensed User
Hi, yes this might be the case.
Thats why in my final solution delegate the events to main module and then pass back to the right instance by checking the list of insances and IP number.

I just wanted to be sure that is the right solution in such a case, where the port numbers are the same in all devices.

Thank you for the clarification of this behaviour.
 
Top