问题
Can I perform a UDP broadcast packets on my machine? I don't have a network, i just have my cheap linux box. I want to have a server broadcasting a packet and two or more clients in the same machine receiving them. Is that possible? What IP do I use?
@gravyface gave me hope but I tried: 1) server sending to 127.255.255.255:54321 and clients listenting to 0.0.0.0:54321. 2) server sending to 127.255.255.255:54321 and clients listening to 127.0.0.1:54321. 3) server sending to 127.255.255.255:54321 and clients listening to 127.255.255.255:54321. None of them worked! :(
OBS: I am using REUSE_ADDR and SO_BROADCAST options.
回答1:
I am able to confirm that 127.255.255.255 works on Linux (Ubuntu) and it does not work on Mac (Snow Leopard). If you ever find out how to do that on Mac let me know. :)
回答2:
You can use virtual network adapters with different virtual addresses. For example in Windows you can use Microsoft Loopback or TUN/TAP in Linux.
Another solution would be to create an internal network with a Virtualization software such as VirtualBox but this would require a slight faster machine. If you could only afford(since it's a cheap box) a single Virtual Machine you could enable 1-4 network adapters that would allow you to bind your different UDP clients individually on each one of the virtual adapters of your virtual device.
来源:https://stackoverflow.com/questions/12131815/how-to-test-broadcast-udp-packets-in-java-on-the-same-machine