public static void init(final DatagramChannel channel) throws IOException { channel.configureBlocking(false); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); }
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; }
public static void init(final DatagramChannel channel, final InetSocketAddress sendAddress) throws IOException { channel.configureBlocking(false); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.connect(sendAddress); }
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; }
@Override public void setNetworkInterface(NetworkInterface networkInterface) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } else { try { channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface); } catch (IOException e) { throw new ChannelException(e); } } }
@Override public void setTimeToLive(int ttl) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } else { try { channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl); } catch (IOException e) { throw new ChannelException(e); } } }
@Override public void setLoopbackModeDisabled(boolean loopbackModeDisabled) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } else { try { channel.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, loopbackModeDisabled); } catch (IOException e) { throw new ChannelException(e); } } } }
@Override public void open() throws IOException { if (channel == null) { channel = DatagramChannel.open(); if (maxSendBufferSize > 0) { channel.setOption(StandardSocketOptions.SO_SNDBUF, maxSendBufferSize); final int actualSendBufSize = channel.getOption(StandardSocketOptions.SO_SNDBUF); if (actualSendBufSize < maxSendBufferSize) { logger.warn("Attempted to set Socket Send Buffer Size to " + maxSendBufferSize + " bytes but could only set to " + actualSendBufSize + "bytes. You may want to " + "consider changing the Operating System's maximum receive buffer"); } } } if (!channel.isConnected()) { channel.connect(new InetSocketAddress(InetAddress.getByName(host), port)); } }
@Override public void open(final InetAddress nicAddress, final int port, final int maxBufferSize) throws IOException { stopped = false; datagramChannel = DatagramChannel.open(); datagramChannel.configureBlocking(false); if (maxBufferSize > 0) { datagramChannel.setOption(StandardSocketOptions.SO_RCVBUF, maxBufferSize); final int actualReceiveBufSize = datagramChannel.getOption(StandardSocketOptions.SO_RCVBUF); if (actualReceiveBufSize < maxBufferSize) { logger.warn("Attempted to set Socket Buffer Size to " + maxBufferSize + " bytes but could only set to " + actualReceiveBufSize + "bytes. You may want to consider changing the Operating System's " + "maximum receive buffer"); } } // we don't have to worry about nicAddress being null here because InetSocketAddress already handles it datagramChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); datagramChannel.socket().bind(new InetSocketAddress(nicAddress, port)); // if a sending host and port were provided then connect to that specific address to only receive // datagrams from that host/port, otherwise we can receive datagrams from any host/port if (sendingHost != null && sendingPort != null) { datagramChannel.connect(new InetSocketAddress(sendingHost, sendingPort)); } selector = Selector.open(); datagramChannel.register(selector, SelectionKey.OP_READ); }
r.start(); channel=DatagramChannel.open(prot_family).setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl) .bind(new InetSocketAddress(bind_addr, local_port)); sock=channel.socket();
receiveDatagramChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); receiveDatagramChannel.bind(new InetSocketAddress(endPointAddress.getPort())); receiveDatagramChannel.join(endPointAddress.getAddress(), udpChannel.localInterface()); sendDatagramChannel.setOption(StandardSocketOptions.IP_MULTICAST_IF, udpChannel.localInterface()); sendDatagramChannel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, udpChannel.multicastTtl()); multicastTtl = sendDatagramChannel.getOption(StandardSocketOptions.IP_MULTICAST_TTL); sendDatagramChannel.setOption(SO_SNDBUF, Configuration.SOCKET_SNDBUF_LENGTH); receiveDatagramChannel.setOption(SO_RCVBUF, Configuration.SOCKET_RCVBUF_LENGTH);
} else if (option == Options.MULTICAST_TTL) { old = option.cast(channel.getOption(StandardSocketOptions.IP_MULTICAST_TTL)); channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, (Integer) value); } else { return null;
private void openChannel() { time = Timer.timer().now(); try { if (channel != null) { try { channel.close(); } catch (Exception ex) { logger.debug("unable to clean up channel connection", ex); } } channel = DatagramChannel.open(); lastOpenTime = time; channel.configureBlocking(false); channel.setOption(StandardSocketOptions.SO_SNDBUF, bufferSize * 2); } catch (Exception ex) { logger.error("Unable to open channel", ex); } }
public <T> DatagramChannel setOption(SocketOption<T> name, T value) throws IOException { return wrap(delegate.setOption(name, value)); } public DatagramChannel connect(SocketAddress remote) throws IOException {
public GUICaptureBroadcast(InetAddress myIP, byte[] group) throws IOException { NetworkInterface iface = NetworkInterface.getByInetAddress(myIP); System.out.println("Announcing UI stream on " + iface); this.sendAddress = new InetSocketAddress(InetAddress.getByAddress(group), LogDataProtocolSettings.UI_ANNOUNCE_PORT); channel = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true) .setOption(StandardSocketOptions.IP_MULTICAST_IF, iface); ipBuffer.put(HEADER); ipBuffer.put(myIP.getAddress()); }
private DatagramChannel ceateSocket(boolean block, int port) throws IOException { DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET) .setOption(StandardSocketOptions.SO_REUSEADDR, true) .setOption(StandardSocketOptions.IP_MULTICAST_IF, iface) .setOption(StandardSocketOptions.SO_RCVBUF, conf.getSocketReceiveBufferSize()) .setOption(StandardSocketOptions.IP_TOS, conf.getTrafficClass()) .setOption(StandardSocketOptions.IP_MULTICAST_LOOP, conf.isLoopBack()) .setOption(StandardSocketOptions.IP_MULTICAST_TTL, conf.getTtl()) .bind(new InetSocketAddress(port)); channel.configureBlocking(block); return channel; }
public StatsdClient(InetAddress host, int port) throws IOException { _address = new InetSocketAddress(host, port); _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, 4096); setBufferSize((short) 1500); }
@Override public void setNetworkInterface(NetworkInterface networkInterface) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } else { try { channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface); } catch (IOException e) { throw new ChannelException(e); } } }
@Override public void setTimeToLive(int ttl) { if (DetectionUtil.javaVersion() < 7) { throw new UnsupportedOperationException(); } else { try { channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl); } catch (IOException e) { throw new ChannelException(e); } } }