If you do not bind your socket, the kernel should find the source address based on the destination address.
Basically, a route search is performed and the destination interface is determined. After that, the IP address is taken from this interface: the source for your packet.
So, your question turns into doing a route search, as does ip route get .
EDIT
@nos is mentioned using a different socket (UDP) and connecting it to this destination address. Retrieving a locally bound name using getsockname should indicate the source address that will be used for this destination.
source share