Java Code Examples for java.nio.channels.DatagramChannel#write()

The following examples show how to use java.nio.channels.DatagramChannel#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Sender.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    try {
        DatagramChannel dc = DatagramChannel.open();
        ByteBuffer bb = ByteBuffer.allocateDirect(12);
        bb.order(ByteOrder.BIG_ENDIAN);
        bb.putInt(1).putLong(1);
        bb.flip();
        InetAddress address = InetAddress.getLocalHost();
        InetSocketAddress isa = new InetSocketAddress(address, port);
        dc.connect(isa);
        clientISA = dc.getLocalAddress();
        dc.write(bb);
    } catch (Exception ex) {
        e = ex;
    }
}
 
Example 2
Source File: UnknownPortDatagramTest.java    From netcrusher-java with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    DatagramChannel channel = DatagramChannel.open();
    channel.configureBlocking(true);
    channel.connect(new InetSocketAddress(HOSTNAME, PORT_CRUSHER));

    try {
        ByteBuffer bb = ByteBuffer.allocate(1024);
        bb.limit(800);
        bb.position(0);

        channel.write(bb);

        Thread.sleep(1001);
    } finally {
        NioUtils.close(channel);
    }
}
 
Example 3
Source File: UnknownHostDatagramTest.java    From netcrusher-java with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    DatagramChannel channel = DatagramChannel.open();
    channel.configureBlocking(true);
    channel.connect(new InetSocketAddress(HOSTNAME_BIND, PORT_CRUSHER));

    try {
        ByteBuffer bb = ByteBuffer.allocate(1024);
        bb.limit(800);
        bb.position(0);

        channel.write(bb);

        Thread.sleep(1002);
    } finally {
        NioUtils.close(channel);
    }
}
 
Example 4
Source File: EmptyBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void test() throws Exception {
    DatagramChannel dc = DatagramChannel.open();
    InetAddress localHost = InetAddress.getLocalHost();
    dc.bind(new InetSocketAddress(localHost, 0));

    Server server = new Server(dc.getLocalAddress());
    Thread serverThread = new Thread(server);
    serverThread.start();

    try {
        InetSocketAddress isa = new InetSocketAddress(localHost, server.port());
        dc.connect(isa);

        ByteBuffer bb = ByteBuffer.allocateDirect(12);
        bb.order(ByteOrder.BIG_ENDIAN);
        bb.putInt(1).putLong(1);
        bb.flip();

        dc.write(bb);
        bb.rewind();
        dc.write(bb);
        bb.rewind();
        dc.write(bb);

        Thread.sleep(2000);

        serverThread.interrupt();
        server.throwException();
    } finally {
        dc.close();
    }
}
 
Example 5
Source File: MyVpnService.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
private void handshake(DatagramChannel tunnel) throws Exception {
    // To build a secured tunnel, we should perform mutual authentication
    // and exchange session keys for encryption. To keep things simple in
    // this demo, we just send the shared secret in plaintext and wait
    // for the server to send the parameters.

    // Allocate the buffer for handshaking.
    ByteBuffer packet = ByteBuffer.allocate(1024);

    // Control messages always start with zero.
    packet.put((byte) 0).put(mSharedSecret).flip();

    // Send the secret several times in case of packet loss.
    for (int i = 0; i < 3; ++i) {
        packet.position(0);
        tunnel.write(packet);
    }
    packet.clear();

    // Wait for the parameters within a limited time.
    for (int i = 0; i < 50; ++i) {
        Thread.sleep(100);

        // Normally we should not receive random packets.
        int length = tunnel.read(packet);
        if (length > 0 && packet.get(0) == 0) {
            configure(new String(packet.array(), 1, length - 1).trim());
            return;
        }
    }
    throw new IllegalStateException("Timed out");
}
 
Example 6
Source File: NioUdpSession.java    From ymate-platform-v2 with Apache License 2.0 5 votes vote down vote up
private int __doChannelWrite(DatagramChannel channel, ByteBuffer buffer) throws IOException {
    if (channel != null) {
        return channel.write(buffer);
    }
    buffer.reset();
    return 0;
}
 
Example 7
Source File: SmartVpnService.java    From SmartZPN with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void handshake(DatagramChannel tunnel) throws Exception {
    // To build a secured tunnel, we should perform mutual authentication
    // and exchange session keys for encryption. To keep things simple in
    // this demo, we just send the shared secret in plaintext and wait
    // for the server to send the parameters.
    // Allocate the buffer for handshaking.
    ByteBuffer packet = ByteBuffer.allocate(1024);
    if (false){
        // Control messages always start with zero.
        packet.put((byte) 0).put(mSharedSecret).flip();
        // Send the secret several times in case of packet loss.
        for (int i = 0; i < 3; ++i) {
            packet.position(0);
            tunnel.write(packet);
        }
        packet.clear();
    }

    // Wait for the parameters within a limited time.
    for (int i = 0; i < 50; ++i) {
        Thread.sleep(100);
        // Normally we should not receive random packets.
        int length = tunnel.read(packet);
        if (length > 0 && packet.get(0) == 0) {
            configure(new String(packet.array(), 1, length - 1).trim());
            return;
        }
    }
    throw new IllegalStateException("Timed out");
}
 
Example 8
Source File: WriteReceiveUdpPing.java    From aeron with Apache License 2.0 4 votes vote down vote up
private static void measureRoundTrip(
    final Histogram histogram,
    final ByteBuffer buffer,
    final DatagramChannel[] receiveChannels,
    final DatagramChannel writeChannel,
    final AtomicBoolean running)
    throws IOException
{
    for (int sequenceNumber = 0; sequenceNumber < Common.NUM_MESSAGES; sequenceNumber++)
    {
        final long timestampNs = System.nanoTime();

        buffer.clear();
        buffer.putLong(sequenceNumber);
        buffer.putLong(timestampNs);
        buffer.flip();

        writeChannel.write(buffer);

        buffer.clear();
        boolean available = false;
        while (!available)
        {
            if (!running.get())
            {
                return;
            }

            for (int i = receiveChannels.length - 1; i >= 0; i--)
            {
                if (null != receiveChannels[i].receive(buffer))
                {
                    available = true;
                    break;
                }
            }
        }

        final long receivedSequenceNumber = buffer.getLong(0);
        if (receivedSequenceNumber != sequenceNumber)
        {
            throw new IllegalStateException("Data Loss:" + sequenceNumber + " to " + receivedSequenceNumber);
        }

        final long durationNs = System.nanoTime() - buffer.getLong(SIZE_OF_LONG);
        histogram.recordValue(durationNs);
    }

    histogram.outputPercentileDistribution(System.out, 1000.0);
}
 
Example 9
Source File: ReceiveWriteUdpPong.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws IOException
{
    int numChannels = 1;
    if (1 == args.length)
    {
        numChannels = Integer.parseInt(args[0]);
    }

    final ByteBuffer buffer = ByteBuffer.allocateDirect(MTU_LENGTH_DEFAULT);

    final DatagramChannel[] receiveChannels = new DatagramChannel[numChannels];
    for (int i = 0; i < receiveChannels.length; i++)
    {
        receiveChannels[i] = DatagramChannel.open();
        Common.init(receiveChannels[i]);
        receiveChannels[i].bind(new InetSocketAddress("localhost", Common.PING_PORT + i));
    }

    final InetSocketAddress writeAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
    final DatagramChannel writeChannel = DatagramChannel.open();
    Common.init(writeChannel, writeAddress);

    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));

    while (true)
    {
        buffer.clear();

        boolean available = false;
        while (!available)
        {
            if (!running.get())
            {
                return;
            }

            for (int i = receiveChannels.length - 1; i >= 0; i--)
            {
                if (null != receiveChannels[i].receive(buffer))
                {
                    available = true;
                    break;
                }
            }
        }

        final long receivedSequenceNumber = buffer.getLong(0);
        final long receivedTimestamp = buffer.getLong(SIZE_OF_LONG);

        buffer.clear();
        buffer.putLong(receivedSequenceNumber);
        buffer.putLong(receivedTimestamp);
        buffer.flip();

        writeChannel.write(buffer);
    }
}