Udp recreiving multiple ports

Exact that is very I’m looking for cause I need tthe udp receiver in my own class. But according my current understanding the receive handler has to implemented outside. And it is my current understandig that it is possible to iplement more that one udp handler using hypermedias udp lib.

I’m still playing around. I’ll post my solution as soon as I get some usefull results.

After some research and experiments it looks like that it is not possible to use the receive handler inside the extended class using the udp lib from hypermedia. According my current understandig the the receiving handler has to be defined outside of the class and the extended class.

This is my main pde.

String ip       = "localhost";  // the fixed remote IP address
int sendtoport        = 9000;    // the destination port

myUDP myUDP; 

String received =""; 
PFont font; 

void setup() {
  size(400, 300);  
  font = createFont("Arial", 40);
  textFont(font, 20);
  textAlign(LEFT, CENTER);
  myUDP = new myUDP( this, 8889, "localhost" );
  // myUDP.listen( true ); 
  // myUDP.log( true );  

  // myUDP.setReceiveHandler("myhandle");
}

void draw() {
 
  // println("Received somthing: ", received);
  if (received != "") {
    //received = "OK -> " + received;
    //received = "OK"; 
    myUDP.send("OK".getBytes(), ip, sendtoport );
  }

  // Draw the image
  background(0);
  text("Received:" + received, 30, 80, width, height);
}

void myhandle( byte[] data, String ip, int port) { // <– default handler
  {
    String received ="";
    //void MYreceive( byte[] data, String ip, int port ) {   // <-- extended handler
    received= new String(data);
    println( "receive: \""+received+"\" from "+ip+" on port "+port );
  }
}

And inside extendUDP.pde I define my class.

// import UDP library
import hypermedia.net.*;

static public class myUDP extends UDP { 
  PApplet app; 
  String ip;  
  int port; 

  public myUDP(PApplet app, int port, String ip)
  {
    super(app, port, ip);

    //super.log( true );     // <-- printout the connection activity
    super.listen( true );
    super.setReceiveHandler("myhandle");
 
  }
}

In the meantime I’ve implemeted a much simpler udp class on my own and look if I can make use of the handler inside my own class. I’m working on a processing low level lib for DJI Tello quadcopter to be used in a computer club later this year and if I find some sponsors to by the necessary hardware.

Code above is still bogus and may not wor properly. May take some time but I’ll publish a better example on github later this week.

1 Like