B4J Question UDP PacketArrived event in a class

woniol

Active Member
Licensed User
Longtime 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
Longtime 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:
Upvote 0

DonManfred

Expert
Licensed User
Longtime 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...
 
Upvote 0

woniol

Active Member
Licensed User
Longtime 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....
 
Upvote 0

woniol

Active Member
Licensed User
Longtime 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

  • bltest.zip
    2.8 KB · Views: 356
Upvote 0

OliverA

Expert
Licensed User
Longtime 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.
 
Upvote 0

woniol

Active Member
Licensed User
Longtime 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.
 
Upvote 0
Top