Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify network interface in Java [closed]

Tags:

java

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?

like image 702
Arse Isonline Avatar asked Jun 12 '12 15:06

Arse Isonline


2 Answers

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

like image 75
tskuzzy Avatar answered Nov 14 '22 21:11

tskuzzy


NetworkInterface nif = NetworkInterface.getByName("bge0");

see here:

http://docs.oracle.com/javase/tutorial/networking/nifs/definition.html

like image 32
Jonas Adler Avatar answered Nov 14 '22 22:11

Jonas Adler