Udp recreiving multiple ports

I would like to extend the UDP object to allow for listening to multiple ports. Note the use of extends in the class definition.

public class myUDP extends UDP{ 
  String myIp;  
  int myPort; 
  String inMsg;
  boolean inMsgrdy;
  public myUDP( String ip,  int port)
    super(port);               //super(this, ip, port)  this not allowed this should be the UDP constructor
    super.setReceiveHandler("receivea");  //this does nothing
    myIp   = ip;
    myPort = port;
  void receivea( byte[] data) { // <– default handler

my problem is that this can not be used in the super constructer.

uasing set receiveHandler(“receivea”); does not woork either.

1 Like

@jimfah – were you able to resolve your problem?

I might suggest trying composition – https://www.journaldev.com/1325/composition-in-java-example

…so, create a wrapper object MultiUDP that contains a UDP (or an array / list of multiple UDP objects, depending on what you are trying to do).

It is somewhat of an intellectual curiosity. The following works fine. I thought extend would be more general. __

public class myUDP { 
  String myIp;  
  int myPort; 
  UDP inUDP;
  String inMsg;
  boolean inMsgrdy;
  public myUDP( String ip,  int port)
    myIp = ip;
    myPort = port;
    println("new "+port + "  ",ip);
    inUDP = new UDP(this, port); 

I am still unclear of the relationship between java and p3.
1 Like

While the extends keyword is for inheritance, there are many ways to be “more general” – often inheritance is more explicit (and restrictive) by design. There is a long evolution of discussion about the use of composition as a general alternative to inheritance – it is actually the name of a principle of design:

Processing uses the Java language, with additional simplifications such as additional classes and aliased mathematical functions and operations.

In essence, Processing is a Java dialect. It is run through a pre-processor which converts it into Java. That Java then runs natively on JVM. When you write Processing, you are writing Java-with-syntax-changes, and compiling to Java. A common example is color, which after the pre-processor becomes int. Another common example is the keyword public, which is not required by Processing where it is by Java.


Only when color is used as a datatype. It’s unmodified if it’s the function color() for example. :wink:

Indeed, PDE’s pre-processor spreads public almost anywhere, for fields & methods, if they don’t have an access keyword already.

A notable big exception though is class & interface.

Methods inside anonymous instantiations don’t get auto public either.

Unless those anonymous instantiations happen in a field declaration it seems.

1 Like

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
  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 );

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

To whom it my concern, as already anounced, find an extremly simple proof of concept

Now I’ve corrected the sender sketch. Unfortunately I’ve uploaded the wrong files yesterday.

1 Like