我的电脑连接到2个不同的接口,可以访问互联网(有线连接和wifi连接到我的智能手机,具有移动访问权限,用于有线连接丢失的情况)。
当有线连接有互联网连接时,我在DOS提示符下手动断开另一个(移动)连接:
netsh wlan disconnect interface="Wi-Fi"
现在,如果我可以通过有线网络连接到达任何网站,我会每分钟检查一次。
一旦失败,java将再次连接到Wi-Fi连接,以便减少互联网连接:
但问题出现了:一旦有线互联网重新启动并运行,我希望java再次通过该接口地址自动连接并断开" Wi-Fi" (限制成本......)。
我不知道是否可以连接到以预定义IP地址开头的网址,以便找出有线连接何时再次联机。
答案 0 :(得分:0)
最后我自己找到了一个解决方案,如下面的代码所示(此代码作为测试运行,在具有有线和无线连接的桌面上启动,自动导致关闭移动连接,然后手动断开连接有线连接,再次自动打开移动连接。一旦手动重新连接有线连接,移动连接将再次断开连接)。
package testingPackage;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.util.Enumeration;
public class TestInternet {
public static void main(String[] args) {
int internet = 1;
int counter = 0;
while(counter<10){
counter = counter + 1;
int connections = 0;
Socket[] soc = new Socket[10];
try{
NetworkInterface nif = NetworkInterface.getByName("eth1");
//the right name of the wired internet can be found with following code in between /* … */
/*
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface interf = interfaces.nextElement();
if (interf.isUp() && !interf.isLoopback()) {
List<InterfaceAddress> adrs = interf.getInterfaceAddresses();
for (Iterator<InterfaceAddress> iter = adrs.iterator(); iter.hasNext();) {
InterfaceAddress adr = iter.next();
InetAddress inadr = adr.getAddress();
if (inadr instanceof Inet4Address){
NetworkInterface nif1 = NetworkInterface.getByInetAddress(inadr);
System.out.println("L30 = "+nif1);
}
}
}
}
*/
Enumeration<InetAddress> nifAddresses = nif.getInetAddresses();
while (nifAddresses.hasMoreElements()){
soc[connections] = new java.net.Socket();
soc[connections].bind(new InetSocketAddress(nifAddresses.nextElement(), 0));
connections = connections + 1;
}
soc[0].connect(new InetSocketAddress("www.google.com", 80));
if(internet==0){
System.out.println("Internet is BACK, throw out the mobile internet now !");
try{
Runtime.getRuntime().exec(new String[]{"netsh", "wlan", "disconnect", "interface=Wi-Fi"});
internet = 1;
}
catch (Exception e2) {System.out.println("Warning : Could not disconnect the mobile network !"); }
}
else{
System.out.println("Internet is UP");
}
}
catch (Exception e) {
System.out.println("enumeration failed");connections = 0;
System.out.println("Internet is still DOWN, connect to mobile internet now !");
connections = 0;
internet = 0;
//find SSID name, profile name and interface name of the wired and wireless network in DOS :
//netsh wlan show networks : returns the SSID name XXXX
//netsh wlan show profile (REQUIRED): returns the profile name YYYY
//netsh wlan show interfaces : returns the interface name
try{
Runtime.getRuntime().exec(new String[]{"netsh", "wlan", "connect","ssid=XXXX", "name=YYYY", "interface=Wi-Fi"});
}
catch (Exception e2) {System.out.println("Warning : no internet ! Could not connect to mobile network !"); }
}
System.out.println("number of wired connections = "+connections);
try {Thread.sleep(10000);}
catch (InterruptedException e) {System.out.println("no sleep"); }
}
} }