Hello,
I’m trying to send udp to multiple devices. Each device would receive discrete data (no broadcast). My strategy is to have a vector of udpconnections, each initialized in a for loop:
ofApp.h:
vector <ofxUDPManager> udpConnections;
ofApp.cpp:
for(int i = 0; i < 4; i++)
{
ofxUDPManager udp;
udp.Create();
udp.Connect(config.getValue("PI:IP", "192.168.2.4", i).c_str(), 11999);
udp.SetNonBlocking(true);
udpConnections.push_back(udp);
}
The first instance seems to work well. However, the next two instances (with IPs 192.168.2.5
and 192.168.2.6
) return and error of:
[ error ] ofxNetwork: .../of_v0.8.4_osx_release/addons/ofxNetwork/src/ofxUDPManager.cpp: 46 EBADF: invalid socket
Strangely 192.168.2.7
seems to not return that error. I have worked with two UDP connections before, but they were not stored in vector and not initialized in a for loop. What am I missing here? Thanks.
update… i’m currently implementing this inelegantly: four separate UDP connections, initialized individually/manually. It is working.
Hello @jmarsico,
when storing complex objects in STL containers (like std::vector), it’s better to store your object first and then initialise the parameters on the copy that’s in the vector; like this:
for(int i = 0; i < 4; i++)
{
ofxUDPManager udp;
udpConnections.push_back(udp);
udpConnections.back().Create();
udpConnections.back().Connect(config.getValue("PI:IP", "192.168.2.4", i).c_str(), 11999);
udpConnections.back().SetNonBlocking(true);
}
This would also be a good scenario for using pointers:
ofApp.h
// storing shared_ptrs to ofxUDPManager class
vector < std::shared_ptr<ofxUDPManager> > udpConnections;
ofApp.cpp
for(int i = 0; i < 4; i++)
{
// create a shared_ptr. It's automatically cleaned up at exit.
std::shared_ptr<ofxUDPManager> udpRef = std::shared_ptr<ofxUDPManager>(new ofxUDPManager);
udpRef->Create();
udpRef->Connect(config.getValue("PI:IP", "192.168.2.4", i).c_str(), 11999);
udpRef->SetNonBlocking(true);
// store shared_ptr in vector of shared_ptrs
udpConnections.push_back(udpRef);
}
Hope this helps,
Vincent
I am having trouble getting this to work. I am doing the same thing, setting up multiple managers to send messages to different IP addresses. Did you have to do anything special to set this network up?