@Override public DatagramChannelPoolable allocate(Slot slot) throws Exception { DatagramChannel channel = DatagramChannel.open(); channel.connect(new InetSocketAddress(server.getHostName(), server.getPort())); ChannelWriter writer = new ChannelWriter(bufferSize, charset, channel); return new DatagramChannelPoolable(slot, writer, channel, flushStrategy); }
public static void init(final DatagramChannel channel, final InetSocketAddress sendAddress) throws IOException { channel.configureBlocking(false); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); channel.connect(sendAddress); }
@Override protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { if (localAddress != null) { doBind0(localAddress); } boolean success = false; try { javaChannel().connect(remoteAddress); success = true; return true; } finally { if (!success) { doClose(); } } }
@Override protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { if (localAddress != null) { doBind0(localAddress); } boolean success = false; try { javaChannel().connect(remoteAddress); success = true; return true; } finally { if (!success) { doClose(); } } }
@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 protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { if (localAddress != null) { doBind0(localAddress); } boolean success = false; try { javaChannel().connect(remoteAddress); success = true; return true; } finally { if (!success) { doClose(); } } }
/** * Initialize AgentMonitoringListener. * * @param port the port of the local agent * @throws SdkClientException */ public AgentMonitoringListener(int port) throws SdkClientException { try { this.dispatcher = AsynchronousAgentDispatcher.getInstance(); this.dispatcher.init(); channel = DatagramChannel.open(); channel.configureBlocking(false); if (channel.socket().getSendBufferSize() < MAX_BUFFER_SIZE) { channel.socket().setSendBufferSize(MAX_BUFFER_SIZE); } //SO_SNDBUF is just a hint and it is implementation specific if the packet is sent or discarded. maxSize = Math.min(MAX_BUFFER_SIZE, channel.socket().getSendBufferSize()); if (maxSize < MAX_BUFFER_SIZE && LOG.isDebugEnabled()) { LOG.debug(String.format("System socket buffer size %d is less than 8K. Any events larger than the buffer size " + "will be dropped", maxSize)); } channel.connect(new InetSocketAddress("localhost", port)); } catch (Exception e) { throw new SdkClientException("Failed to initialize AgentMonitoringListener", e); } }
dChannel.connect(new InetSocketAddress(sendingHost, sendingPort)); dChannel.register(socketChannelSelector, SelectionKey.OP_READ);
@Override public void run() { byte[] bytes = message.getBytes(Charset.forName("UTF-8")); final ByteBuffer buffer = ByteBuffer.allocate(bytes.length); try (DatagramChannel channel = DatagramChannel.open()) { channel.connect(new InetSocketAddress("localhost", port)); for (int i = 0; i < numMessages; i++) { buffer.clear(); buffer.put(bytes); buffer.flip(); while (buffer.hasRemaining()) { channel.write(buffer); } Thread.sleep(delay); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } catch (InterruptedException e) { LOGGER.error(e.getMessage(), e); } } }
tunnel.connect(server);
@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); }
sendDatagramChannel.connect(connectAddress);
channel.getDatagramChannel().connect(remoteAddress); connected = true;
/** * {@inheritDoc} */ @Override protected int writeDirect(Object message) { try { // Check that we can write into the channel if (!isRegisteredForWrite()) { // We don't have pending writes // First, connect if we aren't already connected if (!((DatagramChannel) channel).isConnected()) { ((DatagramChannel) channel).connect(remoteAddress); } // And try to write the data. We will either write them all, // or none return ((DatagramChannel) channel).write((ByteBuffer) message); } else { LOG.debug("Cannot write"); return -1; } } catch (final IOException e) { LOG.error("Exception while reading : ", e); processException(e); return -1; } }
channel.connect(target);
/** * Initialize AgentMonitoringListener. * * @param port the port of the local agent * @throws SdkClientException */ public AgentMonitoringListener(int port) throws SdkClientException { try { this.dispatcher = AsynchronousAgentDispatcher.getInstance(); this.dispatcher.init(); channel = DatagramChannel.open(); channel.configureBlocking(false); if (channel.socket().getSendBufferSize() < MAX_BUFFER_SIZE) { channel.socket().setSendBufferSize(MAX_BUFFER_SIZE); } //SO_SNDBUF is just a hint and it is implementation specific if the packet is sent or discarded. maxSize = Math.min(MAX_BUFFER_SIZE, channel.socket().getSendBufferSize()); if (maxSize < MAX_BUFFER_SIZE && LOG.isDebugEnabled()) { LOG.debug(String.format("System socket buffer size %d is less than 8K. Any events larger than the buffer size " + "will be dropped", maxSize)); } channel.connect(new InetSocketAddress("localhost", port)); } catch (Exception e) { throw new SdkClientException("Failed to initialize AgentMonitoringListener", e); } }
/** * {@inheritDoc} */ @Override protected boolean connect(DatagramChannel handle, SocketAddress remoteAddress) throws Exception { handle.connect(remoteAddress); return true; }
@Override public void connect(InetSocketAddress address) throws IOException { if (isOpen()) { this.dataChannel.connect(address); } else { throw new IOException("The channel is closed."); } }
@Override public DatagramChannelPoolable allocate(Slot slot) throws Exception { DatagramChannel channel = DatagramChannel.open(); channel.connect(new InetSocketAddress(server.getHostName(), server.getPort())); ChannelWriter writer = new ChannelWriter(bufferSize, charset, channel); return new DatagramChannelPoolable(slot, writer, channel, flushStrategy); }
@Test(expected = NetworkReactiveAuditException.class) public void connect() throws IOException { try (DatagramChannel w = channel.get()) { TestTools.strict.commit(); w.connect(null); } }