use of java.net.SocketException in project webmagic by code4craft.
the class ProxyUtils method getNetworkInterface.
private static String getNetworkInterface() {
String networkInterfaceName = ">>>> modify networkInterface in us.codecraft.webmagic.utils.ProxyUtils";
Enumeration<NetworkInterface> enumeration = null;
try {
enumeration = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e1) {
e1.printStackTrace();
}
while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = enumeration.nextElement();
Enumeration<InetAddress> addr = networkInterface.getInetAddresses();
while (addr.hasMoreElements()) {
String s = addr.nextElement().getHostAddress();
Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
if (s != null && IPV4_PATTERN.matcher(s).matches()) {
networkInterfaceName += networkInterface.toString() + "IP:" + s + "\n\n";
}
}
}
return networkInterfaceName;
}
use of java.net.SocketException 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.SocketException 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);
}
}
use of java.net.SocketException in project openhab1-addons by openhab.
the class AnelUDPConnector method receiveDatagram.
/**
* This is a blocking call for receiving data from the specific UDP port.
*
* @return The received data.
* @throws Exception
* If an exception occurred.
*/
byte[] receiveDatagram() throws Exception {
try {
if (socket == null) {
socket = new DatagramSocket(receivePort);
}
// Create a packet
final DatagramPacket packet = new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE);
// Receive a packet (blocking)
socket.receive(packet);
return Arrays.copyOfRange(packet.getData(), 0, packet.getLength() - 1);
} catch (SocketException e) {
if (e.getMessage() != null && e.getMessage().equals("socket closed")) {
// happens during shutdown
return null;
}
throw new Exception(e);
} catch (IOException e) {
throw new Exception(e);
}
}
use of java.net.SocketException in project openhab1-addons by openhab.
the class NibeHeatPumpUDPConnector method receiveDatagram.
@Override
public byte[] receiveDatagram() throws NibeHeatPumpException {
final int PACKETSIZE = 255;
try {
if (socket == null) {
socket = new DatagramSocket(port);
}
// Create a packet
DatagramPacket packet = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
// Receive a packet (blocking)
socket.receive(packet);
return Arrays.copyOfRange(packet.getData(), 0, packet.getLength());
} catch (SocketException e) {
throw new NibeHeatPumpException(e);
} catch (IOException e) {
throw new NibeHeatPumpException(e);
}
}
Aggregations