Forums > Dev

receiving bytes with UDP

Aug 04 2009 | 6:20 pm

I’m working on a project where I’d like to receive UDP data from a motion capture suit.

I’ve seen a lot of discussion on the forums about sending raw bytes with UDP, but I haven’t seen much in the way of receiving raw bytes with UDP.

Anybody have any C or Java code to do it on Windows (I’ve got XP professional and Max 5.0.7)? I’m pretty new to Max, and I’ve never worked with IP issues before, so any help would be appreciated.


Aug 06 2009 | 3:06 pm

Well, after a bit of work, I’ve managed to piece something together, and I thought I’d post it in case anybody else is trying to do something similar.
If anybody has any improvements or suggestions let me know.
Also, I’m having some issues with Max being unresponsive while the UDP is receiving. I think it could be fixed with threading, but I’m not sure exactly how that would work. If anybody has suggestions, again, let me know.

Here’s the code (note: this receives a packet every time there is a bang. If you wanted to continuously receive data, a loop works too):

import com.cycling74.max.*;

public class testUDP extends MaxObject

private int port;

private static final String[] INLET_ASSIST = new String[]
"inlet 1 help"
private static final String[] OUTLET_ASSIST = new String[]
"outlet 1 help"

public testUDP(Atom[] args)
port = 7374; //default port

declareInlets(new int[]{DataTypes.ALL});
declareOutlets(new int[]{DataTypes.ALL});


declareAttribute("port"); //make port an attribute


public void bang()
// Create a socket to listen on the port.
DatagramSocket dsocket = new DatagramSocket(port);

// Create a buffer to read datagrams into. If a packet is larger than
//this buffer, the excess will simply be discarded!
byte[] buffer = new byte[2048];

// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

// Wait to receive a datagram

byte[] theData = packet.getData();

//parse theData

// Reset the length of the packet before reusing it.

catch (Exception e)

}// end bang method

//Catchall for receiving a message that isn’t a bang
public void anything(String msg, Atom[] args)
post("I received a " + msg + " message.");
if (args.length > 0)
post("It has the following arguments:");
for (int i=0;i

Aug 10 2009 | 10:45 am

Sorry for the negative message rather than a helpful one but you’re in the wrong forum. There’s a java dev forum.

BTW the unreponsiveness will be due to the


code in the bang() method, this blocks until the packet is received so yes you need to use multithreading.

Aug 10 2009 | 11:23 am

i saw this the other day, which might help. Didn’t try it myself

Viewing 4 posts - 1 through 4 (of 4 total)

Forums > Dev