Search in sources :

Example 46 with UnknownHostException

use of java.net.UnknownHostException in project openhab1-addons by openhab.

the class TCPConnector method open.

/**
     * {@inheritDoc}
     **/
public void open() {
    try {
        tcpSocket = new Socket();
        SocketAddress TPIsocketAddress = new InetSocketAddress(ipAddress, tcpPort);
        tcpSocket.connect(TPIsocketAddress, connectTimeout);
        tcpOutput = new OutputStreamWriter(tcpSocket.getOutputStream(), "US-ASCII");
        tcpInput = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
        connected = true;
        // Start the TCP Listener
        TCPListener = new TCPListener();
        TCPListener.start();
    } catch (UnknownHostException exception) {
        logger.error("open(): Unknown Host Exception: ", exception);
        connected = false;
    } catch (SocketException socketException) {
        logger.error("open(): Socket Exception: ", socketException);
        connected = false;
    } catch (IOException ioException) {
        logger.error("open(): IO Exception: ", ioException);
        connected = false;
    } catch (Exception exception) {
        logger.error("open(): Exception: ", exception);
        connected = false;
    }
}
Also used : SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Example 47 with UnknownHostException

use of java.net.UnknownHostException in project logger by orhanobut.

the class Helper method getStackTraceString.

/**
   * Copied from "android.util.Log.getStackTraceString()" in order to avoid usage of Android stack
   * in unit tests.
   *
   * @return Stack trace in form of String
   */
static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }
    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}
Also used : UnknownHostException(java.net.UnknownHostException) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 48 with UnknownHostException

use of java.net.UnknownHostException in project openhab1-addons by openhab.

the class BenqProjectorNetworkTransport method networkConnect.

private boolean networkConnect() {
    logger.debug("Running connection setup");
    try {
        logger.debug("Setting up socket connection to " + this.networkHost + ":" + this.networkPort);
        this.projectorSocket = new Socket(this.networkHost, this.networkPort);
        this.projectorSocket.setSoTimeout(SOCKET_TIMEOUT_MS);
        logger.debug("Setup reader/writer");
        this.projectorReader = new BufferedReader(new InputStreamReader(this.projectorSocket.getInputStream()));
        this.projectorWriter = new PrintWriter(this.projectorSocket.getOutputStream(), true);
        logger.debug("Network connection setup successfully!");
        return true;
    } catch (UnknownHostException e) {
        logger.error("Unable to find host: " + this.networkHost);
    } catch (IOException e) {
        logger.error("IO Exception: " + e.getMessage());
    }
    return false;
}
Also used : InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Example 49 with UnknownHostException

use of java.net.UnknownHostException in project openhab1-addons by openhab.

the class AlarmDecoderBinding method connect.

private synchronized void connect() {
    try {
        // make sure we have disconnected
        disconnect();
        markAllItemsUnupdated();
        if (m_tcpHostName != null && m_tcpPort > 0) {
            m_socket = new Socket(m_tcpHostName, m_tcpPort);
            m_reader = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
            m_writer = new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream()));
            logger.info("connected to {}:{}", m_tcpHostName, m_tcpPort);
            startMsgReader();
        } else if (this.m_serialDeviceName != null) {
            /*
                 * by default, RXTX searches only devices /dev/ttyS* and
                 * /dev/ttyUSB*, and will so not find symlinks. The
                 * setProperty() call below helps
                 */
            updateSerialProperties(m_serialDeviceName);
            CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_serialDeviceName);
            CommPort cp = ci.open("openhabalarmdecoder", 10000);
            if (cp == null) {
                throw new IllegalStateException("cannot open serial port!");
            }
            if (cp instanceof SerialPort) {
                m_port = (SerialPort) cp;
            } else {
                throw new IllegalStateException("unknown port type");
            }
            m_port.setSerialPortParams(m_portSpeed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            m_port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
            m_port.disableReceiveFraming();
            m_port.disableReceiveThreshold();
            m_reader = new BufferedReader(new InputStreamReader(m_port.getInputStream()));
            m_writer = new BufferedWriter(new OutputStreamWriter(m_port.getOutputStream()));
            logger.info("connected to serial port: {}", m_serialDeviceName);
            startMsgReader();
        } else {
            logger.warn("alarmdecoder hostname or port not configured!");
        }
    } catch (PortInUseException e) {
        logger.error("cannot open serial port: {}, it is in use!", m_serialDeviceName);
    } catch (UnsupportedCommOperationException e) {
        logger.error("got unsupported operation {} on port {}", e.getMessage(), m_serialDeviceName);
    } catch (NoSuchPortException e) {
        logger.error("got no such port for {}", m_serialDeviceName);
    } catch (IllegalStateException e) {
        logger.error("got unknown port type for {}", m_serialDeviceName);
    } catch (UnknownHostException e) {
        logger.error("unknown host name :{}: ", m_tcpHostName, e);
    } catch (IOException e) {
        logger.error("cannot open connection to {}", m_connectString);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) SerialPort(gnu.io.SerialPort) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket)

Example 50 with UnknownHostException

use of java.net.UnknownHostException in project openhab1-addons by openhab.

the class AnelUDPConnector method sendDatagram.

/**
     * Send data to the specified address via the specified UDP port.
     *
     * @param data
     *            The data to send.
     * @throws Exception
     *             If an exception occurred.
     */
void sendDatagram(byte[] data) throws Exception {
    if (data == null || data.length == 0) {
        throw new IllegalArgumentException("data must not be null or empty");
    }
    try {
        final InetAddress ipAddress = InetAddress.getByName(host);
        final DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, sendPort);
        final DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        socket.close();
    } catch (SocketException e) {
        throw new Exception(e);
    } catch (UnknownHostException e) {
        throw new Exception("Could not resolve host: " + host, e);
    } catch (IOException e) {
        throw new Exception(e);
    }
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

UnknownHostException (java.net.UnknownHostException)1736 InetAddress (java.net.InetAddress)726 IOException (java.io.IOException)446 InetSocketAddress (java.net.InetSocketAddress)150 ArrayList (java.util.ArrayList)142 SocketException (java.net.SocketException)135 Test (org.junit.Test)122 Socket (java.net.Socket)93 URL (java.net.URL)77 SocketTimeoutException (java.net.SocketTimeoutException)71 HashMap (java.util.HashMap)71 File (java.io.File)67 MalformedURLException (java.net.MalformedURLException)65 NetworkInterface (java.net.NetworkInterface)58 URI (java.net.URI)55 ConnectException (java.net.ConnectException)54 InputStream (java.io.InputStream)53 Inet4Address (java.net.Inet4Address)52 Inet6Address (java.net.Inet6Address)52 List (java.util.List)51