2015-08-30 50 views
1

我的電腦連接到2個不同的接口,可以訪問互聯網(有線連接和WiFi連接到我的智能手機,有移動訪問,用於在有線連接丟失的情況下)。如何檢查通過某個接口地址的Internet是否啓動?

當有線連接的互聯網連接,我手動斷開在DOS提示符下其它(移動)連接:

netsh wlan disconnect interface="Wi-Fi" 

現在我會檢查每分鐘左右,如果我可以通過有線達成任何網站網絡連接。

一旦失敗,java會連接到Wi-Fi連接再次讓互聯網連接的損失是最小的:

但是這纔是我的問題:只要有線網絡運行起來再次,我想讓java自動再次通過該接口地址連接並斷開「Wi-Fi」(以限制成本......)。

我不知道它是否可以連接,以便找出當有線連接在線再次啓動預定義的IP地址的URL。

回答

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"); } 
} 

}}

+0

雖然這種解決方案對我的作品,這不正是我一直在尋找:此代碼只與移動WIFI連接工作。我真正想要的是與PC的移動USB連接,這對我來說似乎更可靠。但直到現在我無法找到java如何自動斷開這種連接。如果有人知道答案,請將其作爲便條貼在這裏! – Veronique

相關問題