This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Bind to specific port doesn't succeed for UDP socket in NDK sendto

Hello,

I am using UDP command server where I use sendto() to send command messages followed by data. Now I have to use common UDP port for each command as my client uses source port field in command messages.

Now I see source port changes for each sendto().

  We have tried specifying in the sin for a receive a zero for port number, which in some implementations is a wild card, but it doesn't seem to have this effect on the NDK.

SO_REUSEPORT is also not working.

Came to below note in NDK developer guide.

"NOTE: The native operation of the socket sendto operation is to connect ®
sendto ® disconnect. Sockets that are not bound to a local IP or local
port would have an ephemeral port selected every time in order to
override this behavior and to ensure that the ephemeral port is not
selected every time it is recommended that customers do a bind to port 0.
This selects the first free port not in use and all subsequent
communication uses the same port."

The first part describes the observed behavior. It grabs an unused port and increments it for each sendto.
The second part, which recommends using a wild card port identifier is not what we want to do. we don't want to bind to a
random port as we need to know that port info before hand.

Let me know if there is any way to bind user-defined port in UDP for NDK stack.

  • Hi Prasad,

    Can you attach a wireshark capture of this problem? Which board are you using? If you are using TIRTOS, can you also try running the udpEcho example and see if the problem is also present there?

    Regards,
    Gilbert
  • Hello Gilbert,

    Sorry for late reply, took time to replicate customer set-up locally.

    When I ran UDP sendto() example without bind I see UDP source port incrementing for every packet as expected.

    Then after using bind() to user-defined port, I see constant bound port for all of transfer. So bind() call was able to attach user-defined port to the socket.

    When i give port no. as zero in bind as mentioned in NDK ref. guide note, it takes free port and stuck to it for whole transfer.

    Now looks like note in NDK developers guide is obsolete as we are able to use bind() for specifying user-defined port.

    I guess this note should be removed.

  • The note in the developers guide serves more to point out how sendto() behaves when not bound to a specific port and a simple solution is provided (bind to port 0) to avoid sending to random ports each time. The behavior you were seeing (with the exception of binding to a specific port) is exactly what is described in the note. You do make a point that it may be better to update the note to mention the possibility of binding to a specific unused port.

    Regards,
    Gilbert