# Udp recreiving multiple ports

**URL:** <https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516>\
**Category:** Libraries\
**Created:** [January 14, 2019, 6:15pm UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516 "2019-01-14T18:15:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimfah](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jimfah](https://discourse.processing.org/u/jimfah)\
**Post date:** [January 14, 2019, 6:15pm UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/1 "2019-01-14T18:15:10Z")

</div>

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

* * *

```auto
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;
    super.log(true);
  }
  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.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 22, 2019, 12:28am UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/2 "2019-01-22T00:28:18Z")

</div>

@jimfah – were you able to resolve your problem?

I might suggest trying composition – [https://www.journaldev.com/1325/composition-in-java-example](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).

---

<div class="post-metadata">

**Author:** ![jimfah](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jimfah](https://discourse.processing.org/u/jimfah)\
**Post date:** [January 24, 2019, 2:41pm UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/3 "2019-01-24T14:41:08Z")

</div>

It is somewhat of an intellectual curiosity. The following works fine. I thought _extend_ would be more general. \_\_

```
publicclassmyUDP { 
  StringmyIp;  
  intmyPort; 
  UDP inUDP;
  StringinMsg;
  booleaninMsgrdy;
  
  publicmyUDP(Stringip,intport)
  {
    myIp = ip;
    myPort = port;
    println("new "+port +" ",ip);
    inUDP = newUDP(this, port); 
    inUDP.log(false);
    inUDP.listen(true);
```
  
I am still unclear of the relationship between java and p3.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 25, 2019, 1:11am UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/4 "2019-01-25T01:11:40Z")

</div>

> [@jimfah](#):
>
> I thought _extend_ would be more general.

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:

> **[Composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance)**
>
> Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should favor polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) over inheritance from a base or parent class. Ideally all reuse can be achieved by assembling existing components, but in practice inheritance is often needed to make new ones. Therefore inheritance and object composi An...

> [@jimfah](#):
>
> I am still unclear of the relationship between java and p3.

> Processing uses the [Java language](https://en.wikipedia.org/wiki/Java_(programming_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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 25, 2019, 1:46am UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/5 "2019-01-25T01:46:10Z")

</div>

> [@jeremydouglass](#):
>
> A common example is `color`, which after the pre-processor becomes `int`.

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

> [@jeremydouglass](#):
>
> Another common example is the keyword `public` , which is not required by Processing where it is by Java.

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.

---

<div class="post-metadata">

**Author:** ![f42ter](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f42ter/32/16793_2.png) [@f42ter](https://discourse.processing.org/u/f42ter)\
**Post date:** [February 8, 2019, 4:05pm UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/6 "2019-02-08T16:05:09Z")

</div>

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.

```auto
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.

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![f42ter](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f42ter/32/16793_2.png) [@f42ter](https://discourse.processing.org/u/f42ter)\
**Post date:** [February 11, 2019, 8:45pm UTC](https://discourse.processing.org/t/udp-recreiving-multiple-ports/7516/7 "2019-02-11T20:45:28Z")

</div>

To whom it my concern, as already anounced, find an extremly simple proof of concept  
[https://github.com/f41ardu/UDPExamples](https://github.com/f41ardu/UDPExamples)

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