Example usage for java.net SocketException toString

List of usage examples for java.net SocketException toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:ntp.NTPClient.java

public static void main(String[] args) {
    Properties defaultProps = System.getProperties(); //obtiene las "properties" del sistema
    defaultProps.put("java.net.preferIPv6Addresses", "true");//mapea el valor true en la variable java.net.preferIPv6Addresses
    if (args.length == 0) {
        System.err.println("Usage: NTPClient <hostname-or-address-list>");
        System.exit(1);//from  w w w. j a v a  2s  .c om
    }

    Promedio = 0;
    Cant = 0;
    NTPUDPClient client = new NTPUDPClient();
    // We want to timeout if a response takes longer than 10 seconds
    client.setDefaultTimeout(10000);
    try {
        client.open();
        for (String arg : args) {
            System.out.println();
            try {
                InetAddress hostAddr = InetAddress.getByName(arg);
                System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
                TimeInfo info = client.getTime(hostAddr);
                processResponse(info);
            } catch (IOException ioe) {
                System.err.println(ioe.toString());
            }
        }
    } catch (SocketException e) {
        System.err.println(e.toString());
    }

    client.close();
    System.out.println("\n Pomedio " + (Promedio / Cant));
}

From source file:t5practica9_clienteftp.Main.java

public static void main(String[] args) throws IOException {

    //  INTENTA //from   ww  w . j a  va2s. c  om
    try {
        int reply;
        //   creacion del objeto cliente FTP
        clienteFTP = new FTPClient();
        //   conexion del cliente al servidor FTP
        clienteFTP.connect("ftp.rediris.es");
        //   comprobacion de la conexion
        reply = clienteFTP.getReplyCode();
        //  Bucle
        //   Si  Conexion es satisfactoria 
        if (FTPReply.isPositiveCompletion(reply)) {
            //    Abre una sesion con el usuario anonimo
            //    necesita identificarse  
            clienteFTP.login(usuario, password);
            //    Lista las carpetas de 1 nivel del servidor FTP
            System.out.println("Carpetas disponibles en el Servidor");
            //  Arrays de String para almacenar la lista de nombres 
            nombreCarpeta = clienteFTP.listNames();
            //  Bucle
            //    Para  Recorrer toda la carpeta del Servidor FTP
            for (int i = 0; i < nombreCarpeta.length; i++) {
                //    Mostrar por pantalla cada espacio de memoria 
                //     que posee un dato
                System.out.println(nombreCarpeta[i]);
            }
            //    Nombre con el que se se va a recuperarse el fichero
            //    remoto del servidor FTP
            ficheroObtenido = new FileOutputStream(nombreFichero);
            //    Mensaje para mostrar el fichero descargado
            System.out
                    .println("\n Descargando el fichero " + nombreFichero + " de la carpeta : " + rutaFichero);
            //    Recupera el contenido del fichero en el Servidor y lo escribe
            //    en el fichero del directorio del proyecto
            clienteFTP.retrieveFile("/" + rutaFichero + "/" + nombreFichero, ficheroObtenido);
            //    Cierra el nuevo fichero
            ficheroObtenido.close();
            //    Cierra la conexion con el Servidor
            clienteFTP.disconnect();
            //  Mostrar por pantalla 
            System.out.println("");
            System.out.println("Descarga finalizada correctamente");
            System.out.println("");
        } else {
            //    desconectado
            clienteFTP.disconnect();
            System.out.println("");
            System.out.println("FTP ha rechazado la conexion establecida");
            System.out.println("");
            //  Salida incondicional 
            System.out.println(1);
        }
        //   Captura la excepcion
    } catch (SocketException ex) {
        //   error de Socket
        System.out.println("Error Socket  " + ex.toString());
    } catch (IOException ex) {
        System.out.println("Error I/O  " + ex.toString());
    }

}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*from   www .  j a  va 2 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()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getIpAddress() {
    try {//  w  w w.  ja v  a 2 s.  c  om
        for (Enumeration<?> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = (NetworkInterface) en.nextElement();
            for (Enumeration<?> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {

                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("system", ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from ww  w  . j  ava2s. c o m
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface intf = en.nextElement();
            String name = intf.getName();
            if (name.compareTo("eth0") == 0 || name.compareTo("wlan0") == 0) {
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress.getClass() == Inet4Address.class) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("MY", ex.toString());
    }
    return null;
}

From source file:Main.java

/**
 * Get the local IP address./*from   w  ww.  j av a  2 s . c  o  m*/
 * 
 * @return string containing the current local IP address
 */
public static 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();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        android.util.Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

public static HashMap<String, ArrayList<String>> getLocalIpAddresses() {
    try {//from  ww  w  .  j a v a2  s .  com
        HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();

            String name = intf.getName();
            ArrayList<String> addresses = new ArrayList<String>();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    addresses.add(inetAddress.getHostAddress().toString());
                }
            }
            result.put(name, addresses);
        }
        return result;
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    try {//from  w w w . jav  a2  s .co  m
        String ipv4;

        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            if (ni.getName().toLowerCase().contains("usbnet"))
                continue;
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }

        }

    } catch (SocketException ex) {
        Log.d("socket_err", ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/* w w  w .j a  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.isLinkLocalAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return null;

}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*  w  w  w  .ja v a 2 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()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        System.out.println("WifiPreference IpAddress" + ex.toString());
    }
    return null;
}