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