OK, I went a bit crazy before I realised this is what was happening. I need to set the source port of my UDP message as the unit I am communicating with uses the source port as the port it replies to.
It seemed easier to use ofxPocoNetwork to make these changes as there was less code so I added this method.
int UDPClient::sendMessageTo(ofBuffer& buffer, int port) {
if(connected) {
//here is the line I changed
int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
try {
socketAddress = new Poco::Net::SocketAddress(sendIpAddress, port);
int sent = socket->sendTo(buffer.getData(), buffer.size(), *socketAddress);
//int sent = socket->sendTo(buffer, sendSize, *socketAddress);
return sent;
} catch (Poco::Exception &e) {
ofLogError() << "* UDPClient send fail 1";
return 0;
} catch(std::exception& exc) {
ofLogError() << "* UDPClient send fail 2";
return 0;;
} catch (...) {
ofLogError() << "* UDPClient send fail 3";
return 0;
}
}
return 0;
}
But I think that is the wrong approach and I need to set the address of the datagram here in the connect method
void UDPClient::connect(string ipAddr, int port) {
// setup tcp poco client
socketAddress = new Poco::Net::SocketAddress(ipAddr, port);
socket = new Poco::Net::DatagramSocket(Poco::Net::IPAddress::IPv4);
sendIpAddress =ipAddr;
try {
// client must connect to server
socket->connect(*socketAddress);
connected = true;
ofLog() << "UDPClient connected";
ofLog() << "Max receive size: " << socket->getReceiveBufferSize();
ofLog() << "Max send size: " << socket->getSendBufferSize(); // 9216
} catch (Poco::Exception& exc) {
disconnect();
ofLog() << "UDPClient Error: could not create socket- " << exc.displayText();
}
}
I got some info from stack overflow here
This seems to imply I need to change the socket initialisation here
void UDPClient::connect(string ipAddr, int port) {
// setup tcp poco client
socketAddress = new Poco::Net::SocketAddress(ipAddr, port);
socket = new Poco::Net::DatagramSocket(Poco::Net::IPAddress::IPv4);
But I really don’t understand how I can do that - I messed with it for a few hours but the syntax is over my head.
If anyone knows how to add this functionality I would really appreciate it, or if there is another simple library that someone has an addon for that would be also great.
maybe
@trentbrooks knows as that is where this addon came from.
Cheers
Fred