Example usage for java.net SocketException printStackTrace

List of usage examples for java.net SocketException printStackTrace

Introduction

In this page you can find the example usage for java.net SocketException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static NetworkInterface getActiveNetworkInterface(Context c) {
    try {//ww w .j  av  a2  s . c o  m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();

            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (inetAddress.isLoopbackAddress())
                    break; // break inner loop, continue with outer loop

                return intf; // this is not the loopback and it has an IP address assigned
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    // nothing found
    return null;
}

From source file:Main.java

public static String getIpAddress() {
    try {//from w w  w  . ja v a 2s .  c om
        Enumeration en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface ex = (NetworkInterface) en.nextElement();
            Enumeration enumIpAddr = ex.getInetAddresses();

            while (enumIpAddr.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }

        return null;
    } catch (SocketException var4) {
        var4.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String GetIPAdresses_JAVA() {
    String allIPAdresses = "";
    Enumeration<NetworkInterface> e = null;

    try {//from   ww  w. j ava 2 s. c  om
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }

    if (e != null) {
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                allIPAdresses += (i.getHostAddress() + "\n");
            }
        }
    }

    return allIPAdresses;
}

From source file:Main.java

public static String getIPAdd() {
    try {//ww  w  . ja v  a 2 s .c om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static InetAddress getLocalInetAddress() {
    InetAddress ip = null;/* w w  w .  j a va 2  s. co m*/
    try {
        Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
        while (en_netInterface.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();
            Enumeration<InetAddress> en_ip = ni.getInetAddresses();
            while (en_ip.hasMoreElements()) {
                ip = en_ip.nextElement();
                if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                    break;
                else
                    ip = null;
            }
            if (ip != null) {
                break;
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return ip;
}

From source file:Main.java

public static String getIpAddress(boolean alwaysGetWifi) {

    try {/*w w  w.j a  va2s .c  o  m*/
        Enumeration<NetworkInterface> enmNetI = NetworkInterface.getNetworkInterfaces();
        while (enmNetI.hasMoreElements()) {
            NetworkInterface networkInterface = enmNetI.nextElement();
            boolean b = alwaysGetWifi
                    ? networkInterface.getDisplayName().equals("wlan0")
                            || networkInterface.getDisplayName().equals("eth0")
                    : true;
            if (b) {
                Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
                while (inetAddressEnumeration.hasMoreElements()) {
                    InetAddress inetAddress = inetAddressEnumeration.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return "net name is " + networkInterface.getDisplayName() + ",ip is "
                                + inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static String getGPRS_IP() {
    try {/*from   w w w.  j  ava 2s  .  c o  m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enu = intf.getInetAddresses(); enu.hasMoreElements();) {
                InetAddress inetAddress = enu.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    if (inetAddress.getHostAddress().toString().contains("::")) {
                        continue;
                    }
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static String GetWantedIPAdress_JAVA() {
    String wantedIP = "";
    Enumeration<NetworkInterface> e = null;

    try {//w  w  w  .  j a  va2  s.c  o m
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }

    if (e != null) {
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                if (i.getHostAddress().toString().contains("192.168.")
                        || i.getHostAddress().toString().startsWith("10.")) {
                    wantedIP = i.getHostAddress();
                    break;
                }
            }
        }
    }

    return wantedIP;
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null && wifiInfo.getIpAddress() != 0) {
        return android.text.format.Formatter.formatIpAddress(wifiInfo.getIpAddress());
    } else {//from w w  w . j a va2 s.c  om
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        String ipAddress = inetAddress.getHostAddress();
                        if (!TextUtils.isEmpty(ipAddress) && !ipAddress.contains(":")) {
                            return ipAddress;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String getGPRSIP() {
    String ip = null;//from  w ww .  j  a v  a  2s . c om
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            for (Enumeration<InetAddress> enumIpAddr = en.nextElement().getInetAddresses(); enumIpAddr
                    .hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    ip = inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
        ip = null;
    }
    return ip;
}