B4J Question UDP PacketArrived event in a class

Discussion in 'B4J Questions' started by woniol, Aug 24, 2019.

  1. woniol

    woniol Active Member Licensed User

    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.

    PS. Please move it to B4J questions section
  2. Erel

    Erel Administrator Staff Member Licensed User

    The event will be raised in the class instance where the UDPSocket was initialized.

    You can add a global string variable and set it to a different value in each instance. It will help you distinguish between the instances.
  3. woniol

    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: Aug 26, 2019
  4. woniol

    woniol Active Member Licensed User

    Is there an example of UDP usage in class, available on the forum?
  5. DonManfred

    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...
  6. woniol

    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....
  7. Erel

    Erel Administrator Staff Member Licensed User

    You did something wrong. This is all that we can say based on the information you provided.

    Build a small project with the UDP related code and upload it.
  8. woniol

    woniol Active Member Licensed User

    Ok, thank you, I'll try to prepare an example.
  9. woniol

    woniol Active Member Licensed User

    Here is the example.
    The log output I get is the following:
    Init d
    Auth d
    Init d2
    Auth d2
    Udpsock_PacketArrived instance: from host:
    Udpsock_PacketArrived instance: from host:
    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.

    Attached Files:

  10. OliverA

    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.
    Erel likes this.
  11. woniol

    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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice