Java Code Examples for java.nio.channels.DatagramChannel#setOption()

The following examples show how to use java.nio.channels.DatagramChannel#setOption() . 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: ChannelListener.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private DatagramChannel createAndBindDatagramChannel(final InetAddress nicIPAddress, final int port, final int receiveBufferSize)
        throws IOException {
    final DatagramChannel dChannel = DatagramChannel.open();
    dChannel.configureBlocking(false);
    if (receiveBufferSize > 0) {
        dChannel.setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize);
        final int actualReceiveBufSize = dChannel.getOption(StandardSocketOptions.SO_RCVBUF);
        if (actualReceiveBufSize < receiveBufferSize) {
            LOGGER.warn(this + " attempted to set UDP Receive Buffer Size to "
                    + receiveBufferSize + " bytes but could only set to " + actualReceiveBufSize
                    + "bytes. You may want to consider changing the Operating System's "
                    + "maximum receive buffer");
        }
    }
    dChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    dChannel.bind(new InetSocketAddress(nicIPAddress, port));
    return dChannel;
}
 
Example 2
Source File: Network.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** Try to listen to multicast messages
 *  @param udp UDP channel that should listen to multicast messages
 *  @param port Port to use
 *  @return Local multicast address, or <code>null</code> if no multicast support
 */
public static InetSocketAddress configureMulticast(final DatagramChannel udp, final int port)
{
    try
    {
        final NetworkInterface loopback = getLoopback();
        if (loopback != null)
        {
            final InetAddress group = InetAddress.getByName(PVASettings.EPICS_PVA_MULTICAST_GROUP);
            final InetSocketAddress local_broadcast = new InetSocketAddress(group, port);
            udp.join(group, loopback);

            logger.log(Level.CONFIG, "Multicast group " + local_broadcast + " using network interface " + loopback.getDisplayName());
            udp.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, true);
            udp.setOption(StandardSocketOptions.IP_MULTICAST_IF, loopback);

            return local_broadcast;
        }
    }
    catch (Exception ex)
    {
        logger.log(Level.WARNING, "Cannot configure multicast support", ex);
    }
    return null;
}
 
Example 3
Source File: MarketData.java    From parity-extras with Apache License 2.0 6 votes vote down vote up
public static MarketData open(NetworkInterface multicastInterface,
        InetSocketAddress multicastGroup, InetSocketAddress requestAddress,
        long instrument) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.bind(new InetSocketAddress(multicastGroup.getPort()));
    channel.join(multicastGroup.getAddress(), multicastInterface);
    channel.configureBlocking(false);

    DatagramChannel requestChannel = DatagramChannel.open(StandardProtocolFamily.INET);

    requestChannel.configureBlocking(false);

    return new MarketData(channel, requestChannel, requestAddress, instrument);
}
 
Example 4
Source File: DatagramChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_setOption() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    // There were problems in the past as the number used here was below the minimum for
    // some platforms (b/27821554). It was increased from 1024 to 4096.
    dc.setOption(StandardSocketOptions.SO_SNDBUF, 4096);

    // Assert that we can read back the option from the channel...
    assertEquals(4096, (int) dc.getOption(StandardSocketOptions.SO_SNDBUF));
    // ... and its socket adaptor.
    assertEquals(4096, dc.socket().getSendBufferSize());

    dc.close();
    try {
        dc.setOption(StandardSocketOptions.SO_SNDBUF, 4096);
        fail();
    } catch (ClosedChannelException expected) {
    }
}
 
Example 5
Source File: ChannelListener.java    From nifi with Apache License 2.0 6 votes vote down vote up
private DatagramChannel createAndBindDatagramChannel(final InetAddress nicIPAddress, final int port, final int receiveBufferSize)
        throws IOException {
    final DatagramChannel dChannel = DatagramChannel.open();
    dChannel.configureBlocking(false);
    if (receiveBufferSize > 0) {
        dChannel.setOption(StandardSocketOptions.SO_RCVBUF, receiveBufferSize);
        final int actualReceiveBufSize = dChannel.getOption(StandardSocketOptions.SO_RCVBUF);
        if (actualReceiveBufSize < receiveBufferSize) {
            LOGGER.warn(this + " attempted to set UDP Receive Buffer Size to "
                    + receiveBufferSize + " bytes but could only set to " + actualReceiveBufSize
                    + "bytes. You may want to consider changing the Operating System's "
                    + "maximum receive buffer");
        }
    }
    dChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    dChannel.bind(new InetSocketAddress(nicIPAddress, port));
    return dChannel;
}
 
Example 6
Source File: AnnounceGroupChannel.java    From bt with Apache License 2.0 5 votes vote down vote up
private synchronized DatagramChannel getChannel() throws IOException {
    if (channel == null || !channel.isOpen()) {
        if (shutdown.get()) {
            throw new IllegalStateException("Channel has been shut down");
        }
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(group.getAddress().getAddress());
        DatagramChannel _channel = selector.provider().openDatagramChannel(protocolFamily);
        _channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        // bind to any-local before setting TTL
        int port = group.getAddress().getPort();
        if (protocolFamily == StandardProtocolFamily.INET) {
            _channel.bind(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port));
        } else {
            _channel.bind(new InetSocketAddress(Inet6Address.getByName("[::]"), port));
        }
        int timeToLive = group.getTimeToLive();
        if (timeToLive != 1) {
            _channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, timeToLive);
        }

        for (NetworkInterface iface : networkInterfaces) {
            _channel.join(group.getAddress().getAddress(), iface);
        }

        _channel.configureBlocking(false);
        channel = _channel;
    }
    return channel;
}
 
Example 7
Source File: DatagramCrusherSocketOptions.java    From netcrusher-java with Apache License 2.0 5 votes vote down vote up
void setupSocketChannel(DatagramChannel datagramChannel) throws IOException {
    datagramChannel.setOption(StandardSocketOptions.SO_BROADCAST, broadcast);

    if (rcvBufferSize > 0) {
        datagramChannel.setOption(StandardSocketOptions.SO_RCVBUF, rcvBufferSize);
    }

    if (sndBufferSize > 0) {
        datagramChannel.setOption(StandardSocketOptions.SO_SNDBUF, sndBufferSize);
    }
}
 
Example 8
Source File: Statsd.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private DatagramChannel createDatagramChannel() throws IOException {
	final DatagramChannel channel = DatagramChannel.open();
	// Put this in non-blocking mode so send does not block forever.
	channel.configureBlocking(false);
	// Increase the size of the output buffer so that the size is larger than our buffer size.
	channel.setOption(StandardSocketOptions.SO_SNDBUF, SOCKET_BUFFER_SIZE);
	return channel;
}
 
Example 9
Source File: DatagramSocketSettings.java    From datakernel with Apache License 2.0 5 votes vote down vote up
public void applySettings(@NotNull DatagramChannel channel) throws IOException {
	if (receiveBufferSize != 0) {
		channel.setOption(SO_RCVBUF, receiveBufferSize);
	}
	if (sendBufferSize != 0) {
		channel.setOption(SO_SNDBUF, sendBufferSize);
	}
	if (reuseAddress != DEF_BOOL) {
		channel.setOption(SO_REUSEADDR, reuseAddress != FALSE);
	}
	if (broadcast != DEF_BOOL) {
		channel.setOption(SO_BROADCAST, broadcast != FALSE);
	}
}
 
Example 10
Source File: MoldUDP64.java    From nassau with Apache License 2.0 5 votes vote down vote up
/**
 * Receive messages. Invoke the message listener on each message. Continue
 * until a packet indicating the End of Session is received.
 *
 * @param multicastInterface the multicast interface
 * @param multicastGroup the multicast group
 * @param requestAddress the request address
 * @param listener a message listener
 * @throws IOException if an I/O error occurs
 */
public static void receive(NetworkInterface multicastInterface,
        InetSocketAddress multicastGroup, InetSocketAddress requestAddress,
        MessageListener listener) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.bind(new InetSocketAddress(multicastGroup.getPort()));
    channel.join(multicastGroup.getAddress(), multicastInterface);
    channel.configureBlocking(false);

    DatagramChannel requestChannel = DatagramChannel.open(StandardProtocolFamily.INET);

    requestChannel.configureBlocking(false);

    StatusListener statusListener = new StatusListener();

    try (Selector selector = Selector.open();
            MoldUDP64Client client = new MoldUDP64Client(channel, requestChannel,
                requestAddress, listener, statusListener)) {
        SelectionKey channelKey = channel.register(selector, SelectionKey.OP_READ);

        SelectionKey requestChannelKey = requestChannel.register(selector, SelectionKey.OP_READ);

        while (statusListener.receive) {
            while (selector.select() == 0);

            Set<SelectionKey> selectedKeys = selector.selectedKeys();

            if (selectedKeys.contains(channelKey))
                client.receive();

            if (selectedKeys.contains(requestChannelKey))
                client.receiveResponse();

            selectedKeys.clear();
        }
    }
}
 
Example 11
Source File: DatagramChannels.java    From nassau with Apache License 2.0 5 votes vote down vote up
static DatagramChannel openClientChannel() throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.bind(null);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, loopbackInterface());
    channel.join(multicastGroup(), loopbackInterface());

    return channel;
}
 
Example 12
Source File: DatagramChannels.java    From nassau with Apache License 2.0 5 votes vote down vote up
static DatagramChannel openServerChannel(DatagramChannel clientChannel) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, loopbackInterface());
    channel.connect(new InetSocketAddress(multicastGroup(), getLocalPort(clientChannel)));

    return channel;
}
 
Example 13
Source File: Common.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static void init(final DatagramChannel channel, final InetSocketAddress sendAddress)
    throws IOException
{
    channel.configureBlocking(false);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.connect(sendAddress);
}
 
Example 14
Source File: Common.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void init(final DatagramChannel channel) throws IOException
{
    channel.configureBlocking(false);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
}
 
Example 15
Source File: MarketData.java    From parity with Apache License 2.0 4 votes vote down vote up
static MarketData open(String session, NetworkInterface multicastInterface,
        InetSocketAddress multicastGroup,
        InetSocketAddress requestAddress) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, multicastInterface);
    channel.connect(multicastGroup);

    MoldUDP64Server transport = new MoldUDP64Server(channel, session);

    DatagramChannel requestChannel = DatagramChannel.open();

    requestChannel.bind(requestAddress);
    requestChannel.configureBlocking(false);

    MoldUDP64RequestServer requestTransport = new MoldUDP64RequestServer(requestChannel);

    return new MarketData(transport, requestTransport);
}
 
Example 16
Source File: MarketReporting.java    From parity with Apache License 2.0 4 votes vote down vote up
static MarketReporting open(String session, NetworkInterface multicastInterface,
        InetSocketAddress multicastGroup,
        InetSocketAddress requestAddress) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, multicastInterface);
    channel.connect(multicastGroup);

    MoldUDP64Server transport = new MoldUDP64Server(channel, session);

    DatagramChannel requestChannel = DatagramChannel.open();

    requestChannel.bind(requestAddress);
    requestChannel.configureBlocking(false);

    MoldUDP64RequestServer requestTransport = new MoldUDP64RequestServer(requestChannel);

    return new MarketReporting(transport, requestTransport);
}