Search in sources :

Example 11 with MulticastSocket

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

the class SsdpDiscovery method setUpSocket.

private static MulticastSocket setUpSocket() throws IOException {
    MulticastSocket recSocket = new MulticastSocket(null);
    recSocket.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), PORT));
    recSocket.setTimeToLive(10);
    recSocket.setSoTimeout(1000);
    recSocket.setBroadcast(true);
    return recSocket;
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress)

Example 12 with MulticastSocket

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

the class SsdpDiscovery method sendNotify.

private static void sendNotify(String notifyMessage, InetAddress ia) throws Exception {
    MulticastSocket socket = new MulticastSocket(null);
    try {
        socket.bind(new InetSocketAddress(PORT));
        socket.setTimeToLive(4);
        byte[] data = notifyMessage.toString().getBytes();
        socket.send(new DatagramPacket(data, data.length, new InetSocketAddress(ia, PORT)));
    } catch (Exception e) {
        logger.error("sendNotify", e);
        throw e;
    } finally {
        socket.disconnect();
        socket.close();
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 13 with MulticastSocket

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

the class SsdpDiscovery method retrieveResponse.

static Map<String, Map<String, String>> retrieveResponse() throws Exception {
    String response = null;
    Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
    MulticastSocket recSocket = setUpSocket();
    int i = 0;
    logger.debug("Retrieving response");
    while (i < 10) {
        byte[] buf = new byte[2048];
        DatagramPacket input = new DatagramPacket(buf, buf.length);
        try {
            recSocket.receive(input);
            response = new String(input.getData());
            Map<String, String> parsedResponse = parseResponse(response);
            result.put(parsedResponse.get("IP"), parsedResponse);
        } catch (SocketTimeoutException e) {
            if (i >= 10) {
                break;
            }
            i++;
        }
    }
    logger.debug("Response retrieved: {}", result);
    return result;
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketTimeoutException(java.net.SocketTimeoutException) HashMap(java.util.HashMap) DatagramPacket(java.net.DatagramPacket) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with MulticastSocket

use of java.net.MulticastSocket in project jdk8u_jdk by JetBrains.

the class JdpTestCase method run.

public void run() throws Exception {
    log.fine("Test started.");
    log.fine("Listening for multicast packets at " + connection.address.getHostAddress() + ":" + String.valueOf(connection.port));
    log.fine(initialLogMessage());
    log.fine("Pause in between packets is: " + connection.pauseInSeconds + " seconds.");
    startTime = System.currentTimeMillis();
    timeOut = connection.pauseInSeconds * TIME_OUT_FACTOR;
    log.fine("Timeout set to " + String.valueOf(timeOut) + " seconds.");
    MulticastSocket socket = connection.connectWithTimeout(timeOut * 1000);
    byte[] buffer = new byte[BUFFER_LENGTH];
    DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);
    do {
        try {
            socket.receive(datagram);
            onReceived(extractUDPpayload(datagram));
        } catch (SocketTimeoutException e) {
            onSocketTimeOut(e);
        }
        if (hasTestLivedLongEnough()) {
            shutdown();
        }
    } while (shouldContinue());
    log.fine("Test ended successfully.");
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketTimeoutException(java.net.SocketTimeoutException) DatagramPacket(java.net.DatagramPacket)

Example 15 with MulticastSocket

use of java.net.MulticastSocket in project jdk8u_jdk by JetBrains.

the class ClientConnection method connectWithTimeout.

public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
Also used : MulticastSocket(java.net.MulticastSocket)

Aggregations

MulticastSocket (java.net.MulticastSocket)50 IOException (java.io.IOException)19 DatagramPacket (java.net.DatagramPacket)19 InetSocketAddress (java.net.InetSocketAddress)16 InetAddress (java.net.InetAddress)15 Test (org.junit.Test)11 DatagramSocket (java.net.DatagramSocket)10 SocketException (java.net.SocketException)8 SocketTimeoutException (java.net.SocketTimeoutException)8 UnknownHostException (java.net.UnknownHostException)6 NetworkInterface (java.net.NetworkInterface)5 SocketAddress (java.net.SocketAddress)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 URI (java.net.URI)4 SubsetConfiguration (org.apache.commons.configuration2.SubsetConfiguration)4 ConfigBuilder (org.apache.hadoop.metrics2.impl.ConfigBuilder)4 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 BindException (java.net.BindException)2