UDP source port

I need to specify the source port of a UDP message (this is not the same as destination port, but it is the port it a message is sent from). I need to communicate with some professional hardware, it will reply using whatever is the source port of a message.

I had previously worked around this with Poco:

I am wondering if there is another workaround as Poco is not longer part of OF.

As I read (http://serverfault.com/questions/341263/how-are-udp-source-ports-selected) there is difference in the way windows mac and linux will generate source ports for UDP messages). It would be great to rectify this across platforms, I am approaching this problem for something that needs to run on OSX and Windows.


poco is still present as an addon so any old code relying on it will keep working just by including that addon in your project.

and yes i guess it should be relatively straightforward to add a method to get the source port to the ofxNetwork classes

Cheers, I just found it, will give it a go with ofxPoco.

hi @fresla,

I’ve been in the same situation as you in the past and have done this by binding the to port that I want to remote hardware to respond to, without ofxPoco.

Here I send to port 5555 of the device, the device will reply to my IP address on port 5556.

// send
std::string ip = "";
m_udpSend.Connect(ip.c_str(), 5555);

char buf[2];
buf[0] = 'P';
buf[1] = 'B';

if (m_udpSend.Send(buf, 2))
    printf("Sent %s to IP: %s\n", buf, ip.c_str());

oh yeah i totally forgot about that, using Bind you can select which port you want the socket to listen at

Ok, I’ll do some tests and see how I go. looks like a great and super easy solution.