List of usage examples for java.nio.channels DatagramChannel setOption
public abstract <T> DatagramChannel setOption(SocketOption<T> name, T value) throws IOException;
From source file:Test.java
public static void main(String[] args) throws Exception { NetworkInterface networkInterface = NetworkInterface.getByName("net1"); DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET); dc.setOption(StandardSocketOptions.SO_REUSEADDR, true); dc.bind(new InetSocketAddress(8080)); dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface); InetAddress group = InetAddress.getByName("180.90.4.12"); MembershipKey key = dc.join(group, networkInterface); }
From source file:Main.java
public static void main(String[] args) throws Exception { DatagramChannel server = DatagramChannel.open(); server.bind(null);//from ww w.j a v a 2 s. c o m NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME); server.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); String msg = "Hello!"; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); InetSocketAddress group = new InetSocketAddress(MULTICAST_IP, MULTICAST_PORT); server.send(buffer, group); System.out.println("Sent the multicast message: " + msg); }
From source file:Main.java
public static void main(String[] args) throws Exception { MembershipKey key = null;// w w w . j av a 2 s. c o m DatagramChannel client = DatagramChannel.open(StandardProtocolFamily.INET); NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME); client.setOption(StandardSocketOptions.SO_REUSEADDR, true); client.bind(new InetSocketAddress(MULTICAST_PORT)); client.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); InetAddress group = InetAddress.getByName(MULTICAST_IP); key = client.join(group, interf); System.out.println("Joined the multicast group:" + key); System.out.println("Waiting for a message from the" + " multicast group...."); ByteBuffer buffer = ByteBuffer.allocate(1048); client.receive(buffer); buffer.flip(); int limits = buffer.limit(); byte bytes[] = new byte[limits]; buffer.get(bytes, 0, limits); String msg = new String(bytes); System.out.format("Multicast Message:%s%n", msg); key.drop(); }
From source file:org.apache.nifi.io.nio.ChannelListener.java
private DatagramChannel createAndBindDatagramChannel(final InetAddress nicIPAddress, final int port, final int receiveBufferSize) throws IOException { final DatagramChannel dChannel = DatagramChannel.open(); dChannel.configureBlocking(false);/*from ww w . j a v a 2s . c o m*/ 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; }