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;
}
}
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();
}
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;
}
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);
}
}
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);
}
}
Aggregations