Java 8 DatagramChannel - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DatagramChannel class.

This code example shows how to use the following methods:connect, socket, configureBlocking, register
            socketChannel.socket().setReuseAddress(getSoReuseAddress());
            socketChannel.socket().setOOBInline(getOoBInline());
            socketChannel.socket().setSoLinger(getSoLingerOn(),getSoLingerTime());
            socketChannel.socket().setTrafficClass(getSoTrafficClass());
        } else if (dataChannel!=null) {
            dataChannel.configureBlocking(false);
            dataChannel.socket().setSendBufferSize(getUdpTxBufSize());
            dataChannel.socket().setReceiveBufferSize(getUdpRxBufSize());
            dataChannel.socket().setSoTimeout((int)getTimeout());
            dataChannel.socket().setReuseAddress(getSoReuseAddress());
            dataChannel.socket().setTrafficClass(getSoTrafficClass());
        } 
    } 
 
    private void completeConnect() { 
        //we connected, register ourselves for writing 
        setConnected(true); 
        connecting = false;
        setRequestCount(0); 
        setConnectTime(System.currentTimeMillis());

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:connect, socket, register, configureBlocking
 
		//trigger_buf.put(trigger_bytes); 
	    //trigger_buf.flip(); 
		 
		channel = DatagramChannel.open();
		channel.configureBlocking(false);
		channel.socket().bind(new InetSocketAddress(video_port));
		channel.connect(new InetSocketAddress(drone_addr, video_port));
		 
		 
		selector = Selector.open();
		channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
	} 
 
	private void disconnect() { 
		Log.v("Drone Control", "Video Reader: Told to disconnect");
		try { 
			selector.close();
		} catch (IOException iox) {
			// Ignore 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:isConnected, socket, isOpen, isRegistered
     *  
     * @return 
     */ 
    public String getDatagramChannelState() {
        String state = "";
        if (this.datagramChannel.isOpen() ) {
            state += "OPEN";
        } else { 
            return "CLOSED"; 
        }  
         
        if ( this.datagramChannel.isRegistered() ) {
             state += " REGISTERED";
        } else { 
            return state + " NOT_REGISTERED";
        } 
         
        if ( this.datagramChannel.isConnected()) {
            state += " CONNECTED";
        } else { 

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:socket
    } 
 
    private final DatagramChannel javaChannel;
 
    NioDatagramChannelConfig(NioDatagramChannel channel, DatagramChannel javaChannel) {
        super(channel, javaChannel.socket());
        this.javaChannel = javaChannel;
    } 
 
    @Override 
    public int getTimeToLive() { 
        return (Integer) getOption0(IP_MULTICAST_TTL);
    } 
 
    @Override 
    public DatagramChannelConfig setTimeToLive(int ttl) {
        setOption0(IP_MULTICAST_TTL, ttl);
        return this;
    } 
 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:connect, socket, register, configureBlocking
    } 
 
    private void connect() throws IOException, ClosedChannelException { 
         
        channel = DatagramChannel.open();
        channel.configureBlocking(false);
        channel.socket().bind(new InetSocketAddress(data_port));
        channel.connect(new InetSocketAddress(drone_addr, data_port));
 
        selector = Selector.open();
        channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
    } 
 
    private void disconnect() { 
        try { 
            if (selector.isOpen())
                selector.close();
        } catch (IOException iox) 
        { 
            // ignore 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:isConnected, close
 
	public void close() { 
		super.close(); 
 
		try { 
			if (channel.isConnected()) {
				try { 
					channel.close();
				} 
				catch (IOException e) {
					throw new RuntimeException(e);
				} 
			} 
		} 
		finally { 
			channel = null;
		} 
	} 
} 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:send
 
    private boolean doSend(final String stat) {
        try { 
            final byte[] data = stat.getBytes("utf-8");
            final ByteBuffer buff = ByteBuffer.wrap(data);
            final int nbSentBytes = channel.send(buff, address);
 
            if (data.length == nbSentBytes) {
                return true; 
            } else { 
                log.log(Level.SEVERE, String.format(
                            "Could not send entirely stat %s to host %s:%d. Only sent %i bytes out of %i bytes", stat,
                            address.getHostName(), address.getPort(), nbSentBytes, data.length));
                return false; 
            } 
 
        } catch (IOException e) {
            log.log(Level.SEVERE,
                    String.format("Could not send stat %s to host %s:%d", stat, address.getHostName(),
                                  address.getPort()), e);
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:connect, socket, configureBlocking
 
	public NavDataReader(ARDrone drone, InetAddress drone_addr, int navdata_port) throws IOException {
		this.drone = drone;
 
		channel = DatagramChannel.open();
		channel.configureBlocking(false);
		channel.socket().bind(new InetSocketAddress(navdata_port));
		channel.connect(new InetSocketAddress(drone_addr, navdata_port));
 
		selector = Selector.open();
	} 
 
	private void disconnect() { 
		try { 
			selector.close();
		} catch (IOException iox) {
			// Ignore 
		} 
 
		try { 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:socket
		throw new erjang.NotImplemented(); 
	} 
 
	@Override 
	public void bind(InetSocketAddress localAddress) throws IOException {
		ch.socket().bind(localAddress);
	} 
 
	@Override 
	public 
	SelectableChannel channel() {
		return ch;
	} 
 
	@Override 
	public boolean connect(InetSocketAddress remote) throws IOException {
		ch.socket().connect(remote);
		return true; 
	} 
 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:socket
 
    // Checks that the given DatagramSocket and DatagramChannel are bound to the 
    // same local address. 
    static void check(DatagramSocket ds, DatagramChannel dc) {
        InetAddress expected = ds.getLocalAddress();
        InetAddress actual = dc.socket().getLocalAddress();
        // okay if one bound to 0.0.0.0 and the other to ::0 
        if ((expected.isAnyLocalAddress() != actual.isAnyLocalAddress()) &&
            !expected.equals(actual))
        { 
            throw new RuntimeException("Expected: " + expected + ", actual: " + actual);
        } 
    } 
 
    public static void main(String[] args) throws Exception {
        InetAddress lh = InetAddress.getLocalHost();
        SocketAddress remote = new InetSocketAddress(lh, 1234);
 
        DatagramSocket ds = null;
        DatagramChannel dc = null;
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Java 8 DatagramChannel Questions & Answers:

See Code Examples for Java 8 DatagramChannel Methods: