【问题标题】:How to check if internet through a certain interfaceaddress is up?如何通过某个接口地址检查互联网是否已启动?
【发布时间】:2015-09-05 21:53:07
【问题描述】:

我的电脑连接到 2 个不同的接口,可以访问互联网(有线连接和 wifi 连接到具有移动访问权限的智能手机,以防有线连接丢失)。

当有线连接有互联网连接时,我在 DOS 提示符下手动断开另一个(移动)连接:

netsh wlan disconnect interface="Wi-Fi"

现在我会每隔一分钟左右检查一次我是否可以通过有线互联网连接访问任何站点。

一旦失败,java 将再次连接到 Wi-Fi 连接,以便将互联网连接的丢失降到最低:

但是我的问题来了:一旦有线互联网重新启动并运行,我希望 java 再次通过该接口地址自动连接并断开“Wi-Fi”(以限制成本......)。

我不知道是否可以连接到以预定义 IP 地址开头的 URL,以便了解有线连接何时再次在线。

【问题讨论】:

    标签: java internet-explorer network-interface


    【解决方案1】:

    最后我自己找到了一个解决方案,如以下代码所示(此代码作为测试运行,在具有有线和 wifi 连接的桌面上启动它,这会自动导致关闭移动连接,然后手动断开连接有线连接会自动重新打开移动连接。手动重新连接有线连接后,移动连接会再次断开)。

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

    } }

    【讨论】:

    • 虽然这个解决方案对我有用,但它并不是我想要的:这个代码只适用于移动 WIFI 连接。我真正想要的是与 PC 的移动 USB 连接,这对我来说似乎更可靠。但是直到现在我还找不到java如何自动断开这种连接。如果有人知道答案,请在此处张贴!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 2019-09-03
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多