Search code examples
c++winsock

Send UDP datagram from a specified port WITHOUT binding


I would like to send a UDP datagram from a specified port number WITHOUT binding (ex. when the port is already bound to another socket that I have no control over).

While I do appreciate the OS trying to keep everything nice and clean, and I cherish the bind() functionality, the question is as it is.

How to accomplish this with WSASendTo() or WSASendMsg()?


Solution

  • WSASendTo() will use whichever source IP/Port the socket is currently bound to. If the socket is not bound, WSASendTo() will perform an implicit binding first.

    WSASendMsg() can use the IN_PKTINFO/IN6_PKTINFO structs to specify a source IP and source interface, but AFAIK the only way to specify a source port is through bind().