Search in sources :

Example 21 with SocketException

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;
}
Also used : SocketException(java.net.SocketException) Pattern(java.util.regex.Pattern) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 22 with SocketException

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;
    }
}
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 23 with SocketException

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

Example 24 with SocketException

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);
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 25 with SocketException

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);
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) NibeHeatPumpException(org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Aggregations

SocketException (java.net.SocketException)925 IOException (java.io.IOException)349 InetAddress (java.net.InetAddress)254 NetworkInterface (java.net.NetworkInterface)240 UnknownHostException (java.net.UnknownHostException)153 Socket (java.net.Socket)146 SocketTimeoutException (java.net.SocketTimeoutException)118 ServerSocket (java.net.ServerSocket)114 DatagramSocket (java.net.DatagramSocket)95 Test (org.junit.Test)87 InetSocketAddress (java.net.InetSocketAddress)84 ArrayList (java.util.ArrayList)70 Inet4Address (java.net.Inet4Address)51 DatagramPacket (java.net.DatagramPacket)49 ConnectException (java.net.ConnectException)48 InputStream (java.io.InputStream)41 InterruptedIOException (java.io.InterruptedIOException)41 BindException (java.net.BindException)36 EOFException (java.io.EOFException)33 OutputStream (java.io.OutputStream)32