I am writing a Processing app for my Android phone using the UDP library (
http://ubaa.net/shared/processing/udp/) and i see the following strange behaviour:
with this code
import hypermedia.net.*;
UDP udp;
int fr;
void setup() {
size(screenWidth, screenHeight, A2D);
orientation(PORTRAIT);
udp = new UDP(this, 10023, "192.168.1.18");
print(udp.address());
print(udp.port());
udp.listen(true);
}
void draw() {
}
void receive( byte[] data ) {
String strGet = join(str(char(data)), "");
//println(strGet);
}
i obtain the following output when i run the sketch on device:
opening socket failed!
> address:192.168.1.18, port:10023 [group:null]
> Address already in use
null
-1
if i change the code this way:
import hypermedia.net.*;
UDP udp;
int fr;
void setup() {
size(screenWidth, screenHeight, A2D);
orientation(PORTRAIT);
fr = 0;
}
void draw() {
if (fr == 0) {
udp = new UDP(this, 10023, "192.168.1.18");
print(udp.address());
print(udp.port());
udp.listen(true);
fr++;
}
}
void receive( byte[] data ) {
String strGet = join(str(char(data)), "");
//println(strGet);
}
i have this as output:
192.168.1.18
10023
and all is working as expected.
It seems like the initialization of the UDP object is done twice, maybe the setup() is not the right place to put it.
I would like to know if there are a better solution to this problem.