receiving bytes with UDP

Aug 4, 2009 at 6:20pm

receiving bytes with UDP

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.

Thanks!

#45015
Aug 6, 2009 at 3:06pm

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.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

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

setInletAssist(INLET_ASSIST);
setOutletAssist(OUTLET_ASSIST);

declareAttribute(“port”); //make port an attribute

}

public void bang()
{
try
{
// 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
dsocket.receive(packet);

byte[] theData = packet.getData();

//parse theData

// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);

dsocket.close();
}
catch (Exception e)
{
System.err.println(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
post(args[i].toString());
}
}
}

#162185
Aug 10, 2009 at 10:45am

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

dsocket.receive(packet);

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

#162186
Aug 10, 2009 at 11:23am

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

http://www.esbasel.ch/Downloads/UdpSend+RecvRaw.zip

#162187

You must be logged in to reply to this topic.