Setting the source port number when sending UDP packets?


#1

Hi,

I am trying to use the DatagramSocket class to send data packets over UDP.  Everything seems to be working apart from the source port number seems to be randomly assigned.

Does anyone know how to set the source port when sending UDP packets?

I am currently testing on Win7.

Many thanks,

Chris


#2

Chris, it's normal for a UDP or TCP/IP socket to have an arbitrary outgoing port. It's generally not a problem.

If that doesn't work for you, bind to the port you desire as you open your socket. Sorry - can't tell you off hand how to do that with juce, but that's what you're looking for.

It's somewhat essential with a UDP connection that's intended to 'listen' for external connections.

Bruce

 


#3

Thanks Bruce,

I thought that this might have been the case.  Unfortunately, binding to port in JUCE doesn't seem to set the outgoing port number (only the incoming port number). 

I am currently trying to use the Art-Net protocol and the spec states that the incoming and outgoing port should be 6454.  Having tested with some Art-Net nodes, I have discovered that the nodes work fine as long as they recieve packets on the correct port.  For now, it looks as though I can let the OS choose the outgoing port.

I may have to revisit this in more detail for networks with firewalls but it seems to work well on my current network.

Chris


#4

Yes, the port you send from is invariably irrelevant, except that it can be used as a 'reply address' which would be good for you.

If you wanted to make an Artnet node/receiver, you would need to be on the right port. If Juce's UDP doesn't have a way to bind to an UDP port, that's a missing but required function. Any system that wishes to initiate UDP communications with a Juce app would be unable to.

But - the juce function bindSocketToPort which is called from bindToPort really looks like it does the right thing. Possibly you're not calling it at the right time?

Bruce