ofxUDPManager - bi directionnal

Hello,

I’m trying to work with the ofxUDPManager addon, but i don’t understand if i create an ofxUDPManager object i can use this object to both send and receive like this :

 udpConnection.Create();
udpConnection.Connect("192.168.1.255",11007);
udpConnection.Bind(11006);
udpConnection.SetNonBlocking(true);
udpConnection.SetEnableBroadcast(true);

This seems to didn’t work, because when i receive something i not more able to send.

What is the right way to both send in broadcast and receive through udp ?

Thanks you very much.

the syntax of the udp manager class is kind of strange. upd is not a connection oriented protocol, that means that although you call connect in one of the sides, that it’s only storing the ip an port to which messages will be sent later, also since there’s no real connection the listening side doesn’t know where to send the messages.

in order to be able to send and receive on both sides you need to call both Bind and connect in both managers, also if both are running in the same machine you can’t use the same port when binding and one has to connect to the port that the other is binding

Thank you for your reply, that’s pretty clear. The weird thing is that when i setup the ofxUDPManager object like in my previous post, and i receive a message via the function bellow i’m not able anymore to send something (before receiving anything the sending works) the only solution i found is to recall “udpConnection.Connect(“192.168.1.255”,11007);” just after receiving something, but it’s not elegant at all isn’t it ? is it a bug ?

 void ofApp::update()
{
    char udpMessage[1000];
    udpConnection.Receive(udpMessage,1000);
    string message = udpMessage;
    if(message.length()>0){
        cout << "UDP : " << message << endl;
    }
}

there’s been a lot of fixes on the network addon in the master branch in github so it might have been fixed since the release but this is working for me now:

        int port = ofRandom(15000, 65535);
        ofxUDPManager server;
        server.Create();
        server.SetNonBlocking(false);
        server.Bind(port);
        server.Connect("127.0.0.1", port+1);

        std::string messageSent="message";
        std::vector<char> bufferReceive(messageSent.size(),0);
        std::string messageReceived;

        ofxUDPManager client;
        client.Create();
        client.SetNonBlocking(false);
        client.Bind(port+1);
        client.Connect("127.0.0.1", port);


        client.Send(messageSent.c_str(), messageSent.size());
        auto ret = server.Receive(bufferReceive.data(), messageSent.size());
        if(ret>=0){
            messageReceived.assign(bufferReceive.begin(), bufferReceive.end());
            if(messageSent != messageReceived){
                ofLogError() << "message different";
            }
       }else{
           ofLogError() << "error while receiving"
       }


        server.Send(messageSent.c_str(), messageSent.size());
        bufferReceive.assign(messageSent.size(),0);
        ret = client.Receive(bufferReceive.data(), messageSent.size());
        if(ret>=0){
            messageReceived.assign(bufferReceive.begin(), bufferReceive.end());
            if(messageSent != messageReceived){
                ofLogError() << "message different";
            }
       }else{
           ofLogError() << "error while receiving"
       }


        client.Send(messageSent.c_str(), messageSent.size());
        auto ret = server.Receive(bufferReceive.data(), messageSent.size());
        if(ret>=0){
            messageReceived.assign(bufferReceive.begin(), bufferReceive.end());
            if(messageSent != messageReceived){
                ofLogError() << "message different";
            }
       }else{
           ofLogError() << "error while receiving"
       }


        server.Send(messageSent.c_str(), messageSent.size());
        bufferReceive.assign(messageSent.size(),0);
        ret = client.Receive(bufferReceive.data(), messageSent.size());
        if(ret>=0){
            messageReceived.assign(bufferReceive.begin(), bufferReceive.end());
            if(messageSent != messageReceived){
                ofLogError() << "message different";
            }
       }else{
           ofLogError() << "error while receiving"
       }

Thank you again for your help. Indeed you example works fine. For my project, i need to broadcast message on a local network and i receive from multiple devices on this same local network (this devices broadcast the UDP message too).

Anyway, when I try the code bellow, when I press a key for the first time the message is well sent and received, but when i press again the key, the message is not sent. The weird thing is that it seems send and received looking at the output generated by ofLogError(), but when i sniff the network i see the first key pressed sent, but not the others.

If i manage this with two ofxUDPManager object (one for send and the other for receive) it works, but regarding your advise it must work too with only one ofxUDPManager object.

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
    server.Create();
    server.SetNonBlocking(true);
    server.Bind(11009);
    server.Connect("192.168.1.255", 11009);
    server.SetEnableBroadcast(true);
    
    
    
}

//--------------------------------------------------------------
void ofApp::update(){
    char udpMessage[1000];
    string messageReceived;
    auto ret = server.Receive(udpMessage, 1000);
    if(ret>=0){
        messageReceived.assign(udpMessage);
        if(messageReceived.length()>0){
            ofLogError() << messageReceived;
        }
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    
    
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
    std::string messageSent="message";
    server.Send(messageSent.c_str(), messageSent.size());
    
}

Hi Arturo,

with the code in my last post, by sniffing the network, it seems that the destination port of the sender change after receiving data this is why nobody on the network receive nothing after receiving the first udp packet, so it’s a bit weird. If you have any clue i’ll be really interested.

Cheers,

i added some tests for checking this kind of problems in master some days ago, specifically the UDP one is:

can you send a PR with a test that fails on this problem? this will make it easier for me to test otherwise i’ll give it a try when i have a moment

Hi Arturo,
And sorry for the late reply.
So here is a project with the issue. To reproduce the bug i use “hercule” to receive and send the UDP on another computer that the one running the of application, and i first send an UDP form the of app to the distant computer. Then if i try again, the distant computer didn’t receive anything unless i send something form the distant computer to the of app then it works again.
http://we.tl/nqjK5yjxOx
Thank you again for you help on this subject.

Hi Arturo, and happy new year !
I saw that the link i gave you was dead, so here is a new link : https://dl.dropboxusercontent.com/u/108956169/udpTesting.zip
If you have some time to look at this, it may be helpful.
Thanks again !!!