I want to use a WiFi connection in my Java code. How can I use a specific WiFi network interface in order to download files from the Internet?
In other words, how can I force java to send packets through a specific network interface?
You can query for available network interfaces using the NetworkInterface
class. Then you bind this interface to a Socket using the bind()
method.
NetworkInterface ni = NetworkInterface.getByName("eth0");
Socket socket = new Socket();
socket.bind(ni.getInetAddresses().nextElement());
This is all specified in the Java tutorials: http://docs.oracle.com/javase/tutorial/networking/nifs/definition.html
NetworkInterface nif = NetworkInterface.getByName("bge0");
see here:
http://docs.oracle.com/javase/tutorial/networking/nifs/definition.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With