How to get default gateway using Ethernet, not wifi

心已入冬 提交于 2020-01-14 06:01:48

问题


i'm looking forward to know how to programmatically find the default gateway Address. i've already found default gateway Address for Wifi-Manager(getDhcpInfo()), but i don't find it in Ethernet mode. please. Help me~~


回答1:


If you like to have a two connection at a time , then you can go to the command Prompt and check your routes that are advertised . If the Route has a single default gateway then you may connect to that particular network. If it has two default gateway then you can access both the network which is random

thanks bowmi




回答2:


I'm assuming this is for Google-TV, it might be helpful if you added Google-TV to the title. Is there a way to statically declare the gateway on this appliance? If there is, you could try logging into your router, finding the default gateway, and then directly put the gateway into the configuration options for your device.




回答3:


Here's how you can find the IP Address on Ethernet for Google TV devices:

private static final String IPADDRESS_PATTERN =
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en =
                NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr =
                    intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                String ipAddress = inetAddress.getHostAddress().toString();
                if (!inetAddress.isLoopbackAddress()
                        && validate(ipAddress)) {

                    return ipAddress;
                }
            }
        }
    } catch (SocketException e) {
        // TODO(mjoshi): Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}


/**
 * Validate ip address with regular expression
 * 
 * @param ip ip address for validation
 * @return true valid ip address, false invalid ip address
 */
public boolean validate(final String ip) {
    Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
    Matcher  matcher = pattern.matcher(ip);
    return matcher.matches();
}



回答4:


This is the code we use in Able Remote. Note that we check if the interface is up (will only work with Android 2.3 and later). This is necessary since users reported devices which had multiple IP addresses but only one live at a time:

public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                if (intf.isUp()) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            if (inetAddress instanceof Inet4Address) { // only want ipv4 address
                                return inetAddress.getHostAddress().toString();
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            Log.e(LOG_TAG, "Failed to get the IP address", e);
        }

        return null;
    }


来源:https://stackoverflow.com/questions/10942291/how-to-get-default-gateway-using-ethernet-not-wifi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!