List of usage examples for java.nio.channels DatagramChannel open
public static DatagramChannel open() throws IOException
From source file:me.xingrz.prox.udp.UdpProxySession.java
public UdpProxySession(UdpProxy udpProxy, Selector selector, int sourcePort, InetAddress remoteAddress, int remotePort) throws IOException { super(selector, sourcePort, remoteAddress, remotePort); this.udpProxy = udpProxy; this.serverChannel = DatagramChannel.open(); this.serverChannel.configureBlocking(false); this.serverChannel.socket().bind(new InetSocketAddress(0)); }
From source file:org.lispmob.noroot.IPC.java
public IPC(LISPmobVPNService vpn_service) { this.vpn_service = vpn_service; notifications = new Notifications(this.vpn_service); try {/*www .ja v a 2 s . c om*/ ipc_channel = DatagramChannel.open(); ipc_channel.socket().bind(new InetSocketAddress(ipc_addr, ipc_src_port)); ipc_channel.connect(new InetSocketAddress(ipc_addr, ipc_dst_port)); } catch (Exception e) { e.printStackTrace(); } ipc_thread = new Thread(this, "IPC"); }
From source file:com.facebook.infrastructure.net.UdpConnection.java
public void init() throws IOException { socketChannel_ = DatagramChannel.open(); socketChannel_.socket().setReuseAddress(true); socketChannel_.configureBlocking(false); }
From source file:edu.tsinghua.lumaqq.qq.net.UDPPort.java
/** * /*from w w w . j av a 2 s .c om*/ * * @param policy * ? * @param address * ?? * @throws IOException * ? */ public UDPPort(IConnectionPolicy policy, InetSocketAddress address) throws IOException { super(policy); channel = DatagramChannel.open(); channel.configureBlocking(false); this.remoteAddress = address; }
From source file:com.facebook.infrastructure.net.UdpConnection.java
public void init(int port) throws IOException { // TODO: get TCP port from config and add one. localEndPoint_ = new EndPoint(port); socketChannel_ = DatagramChannel.open(); socketChannel_.socket().bind(localEndPoint_.getInetAddress()); socketChannel_.socket().setReuseAddress(true); socketChannel_.configureBlocking(false); key_ = SelectorManager.getUdpSelectorManager().register(socketChannel_, this, SelectionKey.OP_READ); }
From source file:org.apache.hama.monitor.fd.UDPSensor.java
/** * Constructor for UDP client. Setting up configuration and open * DatagramSocket.// w w w.jav a2 s . c o m */ public UDPSensor(HamaConfiguration configuration) { this.host = configuration.get("bsp.monitor.fd.udp_host", "localhost"); this.port = configuration.getInt("bsp.monitor.fd.udp_port", 16384); HEARTBEAT_INTERVAL = configuration.getInt("bsp.monitor.fd.heartbeat_interval", 1000); DatagramChannel tmp = null; try { tmp = DatagramChannel.open(); } catch (IOException ioe) { LOG.error("Unable to open datagram channel.", ioe); } this.channel = tmp; if (null == this.channel) throw new NullPointerException("Fail to open udp channel."); this.scheduler = Executors.newSingleThreadExecutor(); }
From source file:edu.tsinghua.lumaqq.qq.net.UDPSocks5Port.java
/** * //from ww w.j a v a 2 s . c om * * @param policy * ? * @param serverAddress * ?? * @throws IOException * port */ public UDPSocks5Port(IConnectionPolicy policy, InetSocketAddress serverAddress) throws IOException { super(policy); ready = false; this.remoteAddress = serverAddress; // UDP relaychannel channel = DatagramChannel.open(); channel.configureBlocking(false); channel.socket().bind(new InetSocketAddress(0)); // ? proxy = new Socks5Proxy(this, policy.getProxyUsername(), policy.getProxyPassword(), channel); proxy.setProxyAddress(policy.getProxy()); proxy.setRemoteAddress(serverAddress); proxy.setClientPort(channel.socket().getLocalPort()); }
From source file:gridool.util.net.PoolableSocketChannelFactory.java
private static DatagramChannel createDatagramChannel(final SocketAddress sockAddr, final boolean blocking) { final DatagramChannel ch; try {//ww w . ja v a 2s. c o m ch = DatagramChannel.open(); ch.configureBlocking(blocking); } catch (IOException e) { LOG.error("Failed to open DatagramChannel.", e); throw new IllegalStateException(e); } try { ch.socket().setBroadcast(false); } catch (SocketException e) { LOG.error("Failed to configure socket.", e); throw new IllegalStateException(e); } try { ch.connect(sockAddr); } catch (IOException e) { LOG.error("Failed to connect socket: " + sockAddr, e); throw new IllegalStateException(e); } return ch; }
From source file:com.offbynull.portmapper.pcp.PcpDiscovery.java
private static Map<InetAddress, InetAddress> discoverLocalAddressesToGateways(Set<InetAddress> gateways) throws IOException, InterruptedException { Set<InetAddress> localAddresses = NetworkUtils.getAllLocalIpv4Addresses(); List<DatagramChannel> channels = new ArrayList<>(); final Map<DatagramChannel, InetAddress> bindMap = Collections .synchronizedMap(new HashMap<DatagramChannel, InetAddress>()); final Map<InetAddress, InetAddress> localAddrToGatewayAddrMap = Collections .synchronizedMap(new HashMap<InetAddress, InetAddress>()); try {/* ww w . j a v a 2 s . co m*/ for (InetAddress localAddress : localAddresses) { DatagramChannel unicastChannel = null; try { unicastChannel = DatagramChannel.open(); unicastChannel.configureBlocking(false); unicastChannel.socket().bind(new InetSocketAddress(localAddress, 0)); } catch (IOException ioe) { IOUtils.closeQuietly(unicastChannel); throw ioe; } channels.add(unicastChannel); bindMap.put(unicastChannel, localAddress); } } catch (IOException ioe) { for (DatagramChannel channel : channels) { IOUtils.closeQuietly(channel); } throw ioe; } UdpCommunicator communicator = null; try { communicator = new UdpCommunicator(channels); communicator.startAsync().awaitRunning(); communicator.addListener(new UdpCommunicatorListener() { @Override public void incomingPacket(InetSocketAddress sourceAddress, DatagramChannel channel, ByteBuffer packet) { // make sure version is 2 and error isn't ADDRESS_MISMATCH and we're good to go if (packet.remaining() < 4 || packet.get(0) == 2 && packet.get(4) == PcpResultCode.ADDRESS_MISMATCH.ordinal()) { return; } InetAddress localAddress = bindMap.get(channel); if (localAddress == null) { return; } localAddrToGatewayAddrMap.put(localAddress, sourceAddress.getAddress()); } }); for (DatagramChannel channel : bindMap.keySet()) { for (InetAddress gateway : gateways) { ByteBuffer outBuf = ByteBuffer.allocate(1100); MapPcpRequest mpr = new MapPcpRequest(ByteBuffer.allocate(12), 0, 0, 0, InetAddress.getByName("::"), 0L); mpr.dump(outBuf, bindMap.get(channel)); outBuf.flip(); communicator.send(channel, new InetSocketAddress(gateway, 5351), outBuf.asReadOnlyBuffer()); } } Thread.sleep(5000L); } finally { if (communicator != null) { communicator.stopAsync().awaitTerminated(); } } return new HashMap<>(localAddrToGatewayAddrMap); }
From source file:com.offbynull.portmapper.natpmp.NatPmpDiscovery.java
private static Map<InetAddress, InetAddress> discoverLocalAddressesToGateways(Set<InetAddress> gateways) throws IOException, InterruptedException { Set<InetAddress> localAddresses = NetworkUtils.getAllLocalIpv4Addresses(); List<DatagramChannel> channels = new ArrayList<>(); final Map<DatagramChannel, InetAddress> bindMap = Collections .synchronizedMap(new HashMap<DatagramChannel, InetAddress>()); final Map<InetAddress, InetAddress> localAddrToGatewayAddrMap = Collections .synchronizedMap(new HashMap<InetAddress, InetAddress>()); try {//from www . j ava 2 s. com for (InetAddress localAddress : localAddresses) { DatagramChannel unicastChannel = null; try { unicastChannel = DatagramChannel.open(); unicastChannel.configureBlocking(false); unicastChannel.socket().bind(new InetSocketAddress(localAddress, 0)); } catch (IOException ioe) { IOUtils.closeQuietly(unicastChannel); throw ioe; } channels.add(unicastChannel); bindMap.put(unicastChannel, localAddress); } } catch (IOException ioe) { for (DatagramChannel channel : channels) { IOUtils.closeQuietly(channel); } throw ioe; } UdpCommunicator communicator = null; try { communicator = new UdpCommunicator(channels); communicator.startAsync().awaitRunning(); communicator.addListener(new UdpCommunicatorListener() { @Override public void incomingPacket(InetSocketAddress sourceAddress, DatagramChannel channel, ByteBuffer packet) { new ExternalAddressNatPmpResponse(packet); // should error out if not valid InetAddress localAddress = bindMap.get(channel); if (localAddress == null) { return; } localAddrToGatewayAddrMap.put(localAddress, sourceAddress.getAddress()); } }); ByteBuffer outBuf = ByteBuffer.allocate(16); ExternalAddressNatPmpRequest eanpr = new ExternalAddressNatPmpRequest(); eanpr.dump(outBuf); outBuf.flip(); for (DatagramChannel channel : bindMap.keySet()) { for (InetAddress gateway : gateways) { communicator.send(channel, new InetSocketAddress(gateway, 5351), outBuf.asReadOnlyBuffer()); } } Thread.sleep(5000L); } finally { if (communicator != null) { communicator.stopAsync().awaitTerminated(); } } return new HashMap<>(localAddrToGatewayAddrMap); }