Java Code Examples for java.net.MulticastSocket#setSoTimeout()

The following examples show how to use java.net.MulticastSocket#setSoTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OioDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance from the given {@link MulticastSocket}.
 *
 * @param socket    the {@link MulticastSocket} which is used by this instance
 */
public OioDatagramChannel(MulticastSocket socket) {
    super(null);

    boolean success = false;
    try {
        socket.setSoTimeout(SO_TIMEOUT);
        socket.setBroadcast(false);
        success = true;
    } catch (SocketException e) {
        throw new ChannelException(
                "Failed to configure the datagram socket timeout.", e);
    } finally {
        if (!success) {
            socket.close();
        }
    }

    this.socket = socket;
    config = new DefaultOioDatagramChannelConfig(this, socket);
}
 
Example 2
Source File: CcuDiscoveryService.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private synchronized void startDiscovery() {
    try {
        logger.debug("Starting Homematic CCU discovery scan");
        String configuredBroadcastAddress = networkAddressService.getConfiguredBroadcastAddress();
        if (configuredBroadcastAddress != null) {
            broadcastAddress = InetAddress.getByName(configuredBroadcastAddress);
        }
        if (broadcastAddress == null) {
            logger.warn("Homematic CCU discovery: discovery not possible, no broadcast address found");
            return;
        }
        socket = new MulticastSocket();
        socket.setBroadcast(true);
        socket.setTimeToLive(5);
        socket.setSoTimeout(800);

        sendBroadcast();
        receiveResponses();
    } catch (Exception ex) {
        logger.error("An error was thrown while running Homematic CCU discovery {}", ex.getMessage(), ex);
    } finally {
        scanFuture = null;
    }
}
 
Example 3
Source File: MulticastDiscoveryAgent.java    From tomee with Apache License 2.0 6 votes vote down vote up
Multicast(final Tracker tracker) throws IOException {
    this.tracker = tracker;

    multicast = new MulticastSocket(port);
    multicast.setLoopbackMode(loopbackMode);
    multicast.setTimeToLive(timeToLive);
    multicast.joinGroup(address.getAddress());
    multicast.setSoTimeout((int) heartRate);

    listenerThread = new Thread(new Listener());
    listenerThread.setName("MulticastDiscovery: Listener");
    listenerThread.setDaemon(true);
    listenerThread.start();

    final Broadcaster broadcaster = new Broadcaster();

    timer = new Timer("MulticastDiscovery: Broadcaster", true);
    timer.scheduleAtFixedRate(broadcaster, 0, heartRate);

}
 
Example 4
Source File: OioDatagramChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance from the given {@link MulticastSocket}.
 *
 * @param socket    the {@link MulticastSocket} which is used by this instance
 */
public OioDatagramChannel(MulticastSocket socket) {
    super(null);

    boolean success = false;
    try {
        socket.setSoTimeout(SO_TIMEOUT);
        socket.setBroadcast(false);
        success = true;
    } catch (SocketException e) {
        throw new ChannelException(
                "Failed to configure the datagram socket timeout.", e);
    } finally {
        if (!success) {
            socket.close();
        }
    }

    this.socket = socket;
    config = new DefaultDatagramChannelConfig(this, socket);
}
 
Example 5
Source File: SsdpDiscovery.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Broadcasts a SSDP discovery message into the network to find provided
 * services.
 * 
 * @return The Socket the answers will arrive at.
 * @throws UnknownHostException
 * @throws IOException
 * @throws SocketException
 * @throws UnsupportedEncodingException
 */
private MulticastSocket sendDiscoveryBroacast()
        throws UnknownHostException, IOException, SocketException, UnsupportedEncodingException {
    InetAddress multicastAddress = InetAddress.getByName("239.255.255.250");
    final int port = 1900;
    MulticastSocket socket = new MulticastSocket(port);
    socket.setReuseAddress(true);
    socket.setSoTimeout(130000);
    socket.joinGroup(multicastAddress);
    byte[] requestMessage = DISCOVER_MESSAGE.getBytes("UTF-8");
    DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, multicastAddress,
            port);
    socket.send(datagramPacket);
    return socket;
}
 
Example 6
Source File: UDPTransport.java    From mpegts-streamer with Apache License 2.0 5 votes vote down vote up
private UDPTransport(String address, int port, int ttl, int soTimeout) throws IOException {
	// InetSocketAddress
	inetSocketAddress = new InetSocketAddress(address, port);

	// Create the socket but we don't bind it as we are only going to send data
	// Note that we don't have to join the multicast group if we are only sending data and not receiving
	multicastSocket = new MulticastSocket();
	multicastSocket.setReuseAddress(true);
	multicastSocket.setSoTimeout(soTimeout);
	multicastSocket.setTimeToLive(ttl);
}
 
Example 7
Source File: MulticastSearch.java    From tomee with Apache License 2.0 5 votes vote down vote up
public MulticastSearch(final String host, final int port) throws IOException {
    final InetAddress inetAddress = InetAddress.getByName(host);

    multicast = new MulticastSocket(port);
    multicast.joinGroup(inetAddress);
    multicast.setSoTimeout(500);
}
 
Example 8
Source File: UDP.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void test() throws Exception {
	final String hostname = "google.com";
	final String localhost = "localhost";
	final MulticastSocket datagramSocket = new MulticastSocket();
	datagramSocket.setSoTimeout(10000);
	short ttl = 1;
	final InetAddress receiverAddress = InetAddress.getByName(hostname);
	while (ttl < 100) {
		try {
			byte[] buffer = "0123456789".getBytes();
			datagramSocket.setTimeToLive(ttl++);
			final DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length, receiverAddress, 80);

			datagramSocket.send(sendPacket);

			buffer = new byte[10];
			final DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);

			datagramSocket.receive(receivePacket);
			System.out.println("ttl=" + ttl + " address=" + receivePacket.getAddress().getHostAddress() + " data="
					+ new String(receivePacket.getData()));
			Thread.sleep(1000);
		} catch (final SocketTimeoutException e) {
			System.out.println("timeout ttl=" + ttl);
		}
	}
}
 
Example 9
Source File: Multicast.java    From Bitcoin with Apache License 2.0 5 votes vote down vote up
/**
 * Blocking call
 */
public static boolean recvData(MulticastSocket s, byte[] buffer) throws IOException {
    s.setSoTimeout(100);
    // Create a DatagramPacket and do a receive
    final DatagramPacket pack = new DatagramPacket(buffer, buffer.length);
    try {
        s.receive(pack);
    } catch (SocketTimeoutException e) {
        return false;
    }
    // We have finished receiving data
    return true;
}
 
Example 10
Source File: SsdpDiscovery.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Scans all messages that arrive on the socket and scans them for the
 * search keywords. The search is not case sensitive.
 * 
 * @param socket
 *            The socket where the answers arrive.
 * @param keywords
 *            The keywords to be searched for.
 * @return
 * @throws IOException
 */
private String scanResposesForKeywords(MulticastSocket socket, String... keywords) throws IOException {
    // In the worst case a SocketTimeoutException raises
    socket.setSoTimeout(2000);
    do {
        logger.debug("Got an answer message.");
        byte[] rxbuf = new byte[8192];
        DatagramPacket packet = new DatagramPacket(rxbuf, rxbuf.length);
        socket.receive(packet);
        String foundIp = analyzePacket(packet, keywords);
        if (foundIp != null) {
            return foundIp;
        }
    } while (true);
}
 
Example 11
Source File: ClientConnection.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 12
Source File: ClientConnection.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 13
Source File: ClientConnection.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 14
Source File: ClientConnection.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 15
Source File: ClientConnection.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 16
Source File: ClientConnection.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 17
Source File: ClientConnection.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 18
Source File: ClientConnection.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 19
Source File: ClientConnection.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}
 
Example 20
Source File: ClientConnection.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public MulticastSocket connectWithTimeout(int msTimeOut) throws IOException {
    MulticastSocket socket = new MulticastSocket(port);
    socket.joinGroup(address);
    socket.setSoTimeout(msTimeOut);
    return socket;
}