oscP5 multiple sketches question

#1

Hello. I’m trying to learn about OSC, through the oscP5 library. I have gotten it to work for me in limited circumstances (one Processing sketch sending, one other program receiving (Vuo, in my case).

In trying to understand how it works with many senders and receivers, I made a simple sketch that draws a big dot on the screen, sends its coordinates out over osc, and also listens for incoming messages. When it receives one, it draws a second smaller dot at the coordinates.

It works as expected on its own. I am trying to test multiple senders / receivers by running 3 instances of it under different names (that’s the only way I know of to do it). My understanding of OSC suggests that all three should be able to send and receive based on the address pattern I’m using, so my expected outcome is: if I put a dot on one of the windows, it should get a big dot, and all three should receive and place a small dot at the same coordinates. But I am finding that only one of the three ever receives the messages, and which one appears to be random (once I have started or stopped one of the others, sometimes none receives the messages.

The code I’m using is here:

// osc send & receive tester 
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

boolean recevied; // workaround
float rx, ry; //workaround

void setup() {
  size(400,400);
  frameRate(25);
  background(40);
  noStroke();

  oscP5 = new OscP5(this,12000);
  
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
}

void draw() {
  if (recevied) {
    print("<<< "); // prefix console output with arrows for receiving
    dot(rx,ry,30,100);
    recevied = !recevied;
  }
}

void dot (float x, float y, float d, int a) {
  fill(255,a);
  ellipseMode(CENTER);
  ellipse(x*width,y*height,d,d);
  println("dot size "+d+" at "+x+"-"+y);
}

void mousePressed() {
  print(">>> "); // prefix console output with arrows for sending
  OscMessage myMessage = new OscMessage("/test");
  float x = float(mouseX)/width;
  float y = float(mouseY)/height;
  myMessage.add(x); /* add a float to the osc message */
  myMessage.add(y); /* add a float to the osc message */
  dot(x,y,50,100);
  oscP5.send(myMessage, myRemoteLocation); 
}

void oscEvent(OscMessage theOscMessage) {
  if(theOscMessage.checkAddrPattern("/test")==true) {
    if(theOscMessage.checkTypetag("ff")) {
      float x = theOscMessage.get(0).floatValue();  
      float y = theOscMessage.get(1).floatValue();
      recevied = true;
      rx = x; ry = y;
      return;
    }  
  }
  println("received unrecognized osc message");
}

I do notice an error message in the console for the instances I start after the first, and I’m sure it’s related to my problem:

### [2019/4/13 12:39:7] PROCESS @ OscP5 stopped.
### [2019/4/13 12:39:8] PROCESS @ UdpClient.openSocket udp socket initialized.
### [2019/4/13 12:39:9] ERROR @ UdpServer.start()  IOException, couldnt create new DatagramSocket @ port 12000 java.net.BindException: Address already in use (Bind failed)
### [2019/4/13 12:39:9] INFO @ OscP5 is running. you (10.0.1.7) are listening @ port 12000

So, I expect my mistake is in how I am setting up the sending and listening ports on the oscP5 and netAddress objects, but I am basing my code on the examples, and I guess I don’t understand any further. Either I’m doing that stuff incorrectly or perhaps I can’t expect oscP5 to work this way.

Does anyone have any insights?

Thanks

1 Like

#2

Hey there.
The first issue with this is, that on every computer a specific port can only be bound by one application at a time.

With this line you’re choosing Port 12000 to be bound by your sketch and therefor the first instance you run is blocking that port for everyone else.

So either you go with a single server only and all the other sketches only send data to it or you will need to make each application have its own port.

This line determines where your data is sent to so it is fine to have this on multiple sketches:

1 Like

#3

Ok, I think I understand that. My reading about what OSC can do suggests that you can send a message that will be picked up by multiple receivers (targeted, perhaps, by the oscAddress). My hope is that I can do that, and not have to know how many there are, so that other sketches or programs (e.g, my Vuo program) can come and go, but all listen to some centralized information. Do you think this is possible?

0 Likes

#4

I have used OSC just a few times now and I remember reading something like that too. I’m not sure what exactly they meant though as you always need to enter an IP address and a port to send your data to and that can typcially only be one client. Maybe something to do with multicast?! I don’t know.

I know one way to send to multiple receives though but I cannot recommend using it for all data you’re sending because that is effectively flooding your network with unnecessarily many packets.

Once I did have the need for communication without knowing the server IP address beforehand too. My approach was to send a first OSC message to a network broadcast address (sending to everyone on my network) and in that message I included the senders IP address. Everyone receiving it can then respond to that specific address and if they send their own IP address the original sender now knows where to direct messages to aswell. I got my local networks broadcast address from a function in Unity (I wrote software for android and windows in Unity) but you could enter your address manually. You can read up on broadcast addresses on Wikipedia: https://en.wikipedia.org/wiki/Broadcast_address
Your address might look something like this: 192.168.0.255

You can read a bit about it and if it’s your own network you could also test spamming it if you want to.
If you have any questions feel free to ask but I personally am gone for today and will have to get back to you tomorrow. Have fun :slight_smile:

2 Likes

#5

Very helpful. I read through that as far as my comprehension carried me (not all that far!) and in poking around in the oscP5 stuff, I found its example code for multicasting, and the related https://en.wikipedia.org/wiki/Multicast page,

The example code uses

  oscP5 = new OscP5(this,"239.0.0.1",7777);

to create a multicast socket, which is ‘administratively scoped’. I don’t fully understand that or how to control it, or whether I need to.

I tried modifying my code with the above oscP5 object and no specified netAddress, and it worked exactly as I hoped with 3 instances.

Since my project would run on a small ad-hoc dedicated network, I’m not too worried about overcrowding the network. If I had to run the project on an existing network with other users, I bet the ‘administratively scoped’ part would come in handy.

Thanks for the tips!

2 Likes