If I send a UDP packet containing ‘foo’ like this:
socket = UDPSocket.new
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
socket.send('foo', 0, '<broadcast>', 40001)
then wireshark tells me the packet gets sent correctly, but unfortunately its source address is 192.168.0.3. As my server is listening on localhost:40001, that means it doesn’t receive the packet. I don’t want to let the server listen on 0.0.0.0, as it shouldn’t receive similar UDP requests that are sent within another network. I can make the server listen within the 192.168.0.0/24 network, but later on it will be listening from another network, that is neither localhost nor 192.168.0.0/24, so that doesn’t solve the problem.
Is there a way to choose the source address from which (and the interface via which) the client socket will send its packet?
Have you tried
bind()yet? Generally speaking, if yousocket.bind()to an address (such as127.0.0.1) then your packets should originate from that address. The loopback, and broadcasts for that matter, might be treated specially butbind()would be my first choice to try.