UDP latency issue when using OfxNetwork

I posted this in the “beginners” forums but didn’t get any feedback; maybe that wasn’t the right place to post it.

I’ll give you a basic run-through of my application. I’m using Ableton Live to host MIDI clips containing automation data. Ableton Live sends MIDI CC values to OpenFrameworks Application #1, which reads the MIDI automation data (via OfxMIDI) and sends it out as UDP packets to a specified IP address. Right now this is direct computer 1 to computer 2, in the future likely multicast. On the other end - either an iPhone or another Mac - OpenFrameworks Application #2 reads the UDP packets and draws simple information to the window. Right now it’s simple - automation values from 0-127 change the brightness of the window on the receiving computer. Sounds simple, yes? Unfortunately for this to sync up I need minimal latency - <20 ms, if possible. The applications all work, but there’s about a 5-10 second delay on the receiving end between receiving packets and making changes to the OpenFrameworks window. It is then synchronized to the beats, but it’s all delayed by ~5 seconds.

I’m printing all the MIDI messages in OpenFrameworks Application #1, so the issue is definitely in the UDP Tx -> UDP Rx part of the code.

Below is the relevant code from testApp.cpp.

Application #1: UDP Tx (including MIDI Rx)

void testApp::setup() {  
	ofBackground(255, 255, 255);  
	midiIn.openVirtualPort("Light Array Input");  
	// print received messages to the console  
    //create the socket and set to send to  
    string ipAddress = "";  
    val = 0;  
void testApp::update() {  
void testApp::draw() {  
//    if (val == 127) {  
//        val = 0;  
//    }  
//    else {  
//        val++;  
//    }  
    //string message="";  
    val = midiMessage.value;  
    string message=ofToString(val);  
	int sent = udpConnection.Send(message.c_str(),message.length());  
	ofDrawBitmapString(ofToString(midiMessage.value), 20, 240);  
	text.str(""); // clear  

Application #2: UDP Rx and Display

void testApp::setup(){  
	// listen on the given port  
	cout << "listening for UDP messages\n";  
	mouseX = 0;  
	mouseY = 0;  
	mouseButtonState = "";  
    //create the socket and bind to port 11999  
	ofBackground( 30, 30, 130 );  
    val = 0;  
void testApp::update(){  
    char udpMessage[10];  
	string message=udpMessage;  
    if (message!="") {  
        recieved_val = atof(ofToString(message).c_str());  
        if (recieved_val < 256)  {  
            val = 2*recieved_val;  
void testApp::draw(){  
	string buf;  
	buf = "listening for UDP messages\n";  
	ofDrawBitmapString( buf, 10, 20 );  
    ofBackground(val, val, val);  
    ofDrawBitmapString(ofToString(recieved_val), 30, 40);  

Any ideas on why I’m having this ~5-10 second buffer problem?


Hi and welcome!
No, the place was alright, but you should probably allow more than 24hrs for a reply, especially on a weekend.
Also, please don’t double-post, people will see your question nonetheless. I’ll leave this one open and remove your other identical thread.

Thanks! Sorry about that!

I had a similar problem not too long ago. I was sending UDP from one computer to another, and would notice “lag” building up.

The problem turned out to be that I had misunderstood how UDP works. I thought that when new packets arrived, all the old ones were discarded. This isn’t true. What happens is that the packets pile up until the receiving computer reads them.

So if computer 1 is running at 60 fps, and computer 2 is running at 40 fps, than all the extra packets that computer 1 sends to computer 2 pile up. If all you care about is the latest packet from computer 1, than you should read through all waiting packets until you get to the last one.

here’s the code I used:

        char udpMessage[1000];  
        string message;  
        string tempMessage;  
        bool getNext = true;  
        while (getNext) {  
            if (tempMessage=="") {  
                getNext = false;  
            else {  
                message = tempMessage;  

My god… that fixed it! Thank you SO much!