protected void bind(DatagramSocket socket, SocketAddress localAddress) throws IOException { channel.configureBlocking(true); if (LOG.isDebugEnabled()) { LOG.debug("Binding to address: " + localAddress); } // // We have noticed that on some platfoms like linux, after you close // down // a previously bound socket, it can take a little while before we can // bind it again. // for (int i = 0; i < MAX_BIND_ATTEMPTS; i++) { try { socket.bind(localAddress); return; } catch (BindException e) { if (i + 1 == MAX_BIND_ATTEMPTS) { throw e; } try { Thread.sleep(BIND_ATTEMPT_DELAY); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); throw e; } } } }
/** * @see java.net.DatagramSocket#bind(java.net.SocketAddress) */ @Override public void bind(SocketAddress localAddr) throws SocketException { if (channelImpl.isConnected()) { throw new AlreadyConnectedException(); } super.bind(localAddr); channelImpl.isBound = true; }
ds.bind(new InetSocketAddress((InetAddress) null, port)); logger.info("port {} available", port); return true;
private void doBind0(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { SocketUtils.bind(javaChannel(), localAddress); } else { javaChannel().socket().bind(localAddress); } }
public DatagramServer(Responder responder, SocketAddress addr) throws IOException { String name = "DatagramServer on "+addr; this.responder = responder; this.channel = DatagramChannel.open(); channel.socket().bind(addr); this.transceiver = new DatagramTransceiver(channel); setName(name); setDaemon(true); }
private void doBind0(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { SocketUtils.bind(javaChannel(), localAddress); } else { javaChannel().socket().bind(localAddress); } }
sock.socket().bind(H2O.SELF._key);
public void bind (Selector selector, InetSocketAddress localPort) throws IOException { close(); readBuffer.clear(); writeBuffer.clear(); try { datagramChannel = selector.provider().openDatagramChannel(); datagramChannel.socket().bind(localPort); datagramChannel.configureBlocking(false); selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ); lastCommunicationTime = System.currentTimeMillis(); } catch (IOException ex) { close(); throw ex; } }
if (localAddr != null) { try { bind(localAddr); } catch (SocketException e) { close();
public void connect (Selector selector, InetSocketAddress remoteAddress) throws IOException { close(); readBuffer.clear(); writeBuffer.clear(); try { datagramChannel = selector.provider().openDatagramChannel(); datagramChannel.socket().bind(null); datagramChannel.socket().connect(remoteAddress); datagramChannel.configureBlocking(false); selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ); lastCommunicationTime = System.currentTimeMillis(); connectedAddress = remoteAddress; } catch (IOException ex) { close(); IOException ioEx = new IOException("Unable to connect to: " + remoteAddress); ioEx.initCause(ex); throw ioEx; } }
private void doBind0(SocketAddress localAddress) throws Exception { if (PlatformDependent.javaVersion() >= 7) { SocketUtils.bind(javaChannel(), localAddress); } else { javaChannel().socket().bind(localAddress); } }
@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); }
_udpSocket.socket().setReuseAddress(true); InetSocketAddress isa = new InetSocketAddress(H2O.SELF_ADDRESS, H2O_PORT); _udpSocket.socket().bind(isa);
/** {@inheritDoc} */ public MulticastMessageChannel createUdpServer(final InetSocketAddress bindAddress, final ChannelListener<? super MulticastMessageChannel> bindListener, final OptionMap optionMap) throws IOException { checkShutdown(); final DatagramChannel channel; if (bindAddress != null) { InetAddress address = bindAddress.getAddress(); if (address instanceof Inet6Address) { channel = DatagramChannel.open(StandardProtocolFamily.INET6); } else { channel = DatagramChannel.open(StandardProtocolFamily.INET); } } else { channel = DatagramChannel.open(); } channel.configureBlocking(false); if (optionMap.contains(Options.BROADCAST)) channel.socket().setBroadcast(optionMap.get(Options.BROADCAST, false)); if (optionMap.contains(Options.IP_TRAFFIC_CLASS)) channel.socket().setTrafficClass(optionMap.get(Options.IP_TRAFFIC_CLASS, -1)); if (optionMap.contains(Options.RECEIVE_BUFFER)) channel.socket().setReceiveBufferSize(optionMap.get(Options.RECEIVE_BUFFER, -1)); channel.socket().setReuseAddress(optionMap.get(Options.REUSE_ADDRESSES, true)); if (optionMap.contains(Options.SEND_BUFFER)) channel.socket().setSendBufferSize(optionMap.get(Options.SEND_BUFFER, -1)); channel.socket().bind(bindAddress); final NioUdpChannel udpChannel = new NioUdpChannel(this, channel); ChannelListeners.invokeChannelListener(udpChannel, bindListener); return udpChannel; }
/** * Will bind the DatagramSocket to the passed-in address. * Every call bind will spawn a new thread using the that basically in turn */ private static void bind(final NioDatagramChannel channel, final ChannelFuture future, final InetSocketAddress address) { boolean bound = false; boolean started = false; try { // First bind the DatagramSocket the specified port. channel.getDatagramChannel().socket().bind(address); bound = true; future.setSuccess(); fireChannelBound(channel, address); channel.worker.register(channel, null); started = true; } catch (final Throwable t) { future.setFailure(t); fireExceptionCaught(channel, t); } finally { if (!started && bound) { close(channel, future); } } }
public static void main(String[] args) { try { DatagramSocket s = new DatagramSocket(null); InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000); s.bind(address); } catch (Exception e) { e.printStackTrace(); } }
public boolean open(int bindPort) { close(); try { InetSocketAddress bindSock = new InetSocketAddress(bindPort); ssdpUniSock = new DatagramSocket(null); ssdpUniSock.setReuseAddress(true); ssdpUniSock.bind(bindSock); } catch (Exception e) { //Debug.warning(e); return false; } return true; }
/** * Does not start listener. * Caller must call start(). * * @param parent may be null * @param props ignored for now */ public SAMv3DatagramServer(SAMBridge parent, String host, int port, Properties props) throws IOException { _parent = parent; _server = DatagramChannel.open(); _server.socket().bind(new InetSocketAddress(host, port)); _listener = new I2PAppThread(new Listener(_server), "SAM DatagramListener " + port); _host = host; _port = port; }
/** * {@inheritDoc} */ @Override public void bind(final SocketAddress localAddress) { if (localAddress == null) { // We should at least have one address to bind on throw new IllegalArgumentException("LocalAdress cannot be null"); } // check if the address is already bound if (this.address != null) { throw new IllegalStateException("address " + address + " already bound"); } address = localAddress; LOG.info("binding address {}", localAddress); try { datagramChannel = DatagramChannel.open(); datagramChannel.socket().setReuseAddress(isReuseAddress()); datagramChannel.socket().bind(address); datagramChannel.configureBlocking(false); } catch (IOException e) { throw new MinaRuntimeException("can't open the address " + address, e); } readSelectorLoop.register(false, false, true, false, this, datagramChannel, null); // it's the first address bound, let's fire the event this.fireServiceActivated(); }
try { channel = DatagramChannel.open(); channel.socket().bind(localAddress); Boolean reuseAddress = config.isReuseAddress();