Search in sources :

Example 21 with DatagramChannel

use of java.nio.channels.DatagramChannel in project weicoder by wdcode.

the class UdpUtil method write.

/**
 * nio模式发送数据 接收返回数据
 * @param host 服务器主机
 * @param port 服务器端口
 * @param data 发送数据
 * @param len 接收返回数据长度
 * @return 接收的数据
 */
public static byte[] write(String host, int port, byte[] data, int len) {
    // 实例化Socket
    try (DatagramChannel socket = DatagramChannel.open()) {
        // 连接服务器
        socket.connect(new InetSocketAddress(host, port));
        // 写入数据流
        ChannelUtil.write(socket, data, false);
        // 读取数据
        return ChannelUtil.read(socket, false);
    } catch (IOException e) {
        Logs.error(e);
        return ArrayConstants.BYTES_EMPTY;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException)

Example 22 with DatagramChannel

use of java.nio.channels.DatagramChannel in project aeron by real-logic.

the class SendHackSelectReceiveUdpPing method run.

private void run() throws IOException {
    receiveChannel = DatagramChannel.open();
    Common.init(receiveChannel);
    receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_PORT));
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    final Selector selector = Selector.open();
    receiveChannel.register(selector, OP_READ, this);
    final NioSelectedKeySet keySet = Common.keySet(selector);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(HISTOGRAM, SEND_ADDRESS, buffer, sendChannel, selector, keySet, running);
        HISTOGRAM.reset();
        System.gc();
        LockSupport.parkNanos(1000 * 1000 * 1000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) Selector(java.nio.channels.Selector)

Example 23 with DatagramChannel

use of java.nio.channels.DatagramChannel in project aeron by real-logic.

the class SendReceiveUdpPing method main.

public static void main(final String[] args) throws IOException {
    int numChannels = 1;
    if (1 == args.length) {
        numChannels = Integer.parseInt(args[0]);
    }
    final Histogram histogram = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel[] receiveChannels = new DatagramChannel[numChannels];
    for (int i = 0; i < receiveChannels.length; i++) {
        receiveChannels[i] = DatagramChannel.open();
        init(receiveChannels[i]);
        receiveChannels[i].bind(new InetSocketAddress("localhost", Common.PONG_PORT + i));
    }
    final InetSocketAddress sendAddress = new InetSocketAddress("localhost", Common.PING_PORT);
    final DatagramChannel sendChannel = DatagramChannel.open();
    init(sendChannel);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(histogram, sendAddress, buffer, receiveChannels, sendChannel, running);
        histogram.reset();
        System.gc();
        LockSupport.parkNanos(1000_000_000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Histogram(org.HdrHistogram.Histogram) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 24 with DatagramChannel

use of java.nio.channels.DatagramChannel in project aeron by real-logic.

the class SendSelectReceiveUdpPing method run.

private void run() throws IOException {
    final Histogram histogram = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel receiveChannel = DatagramChannel.open();
    Common.init(receiveChannel);
    receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_PORT));
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    final Selector selector = Selector.open();
    final IntSupplier handler = () -> {
        try {
            buffer.clear();
            receiveChannel.receive(buffer);
            final long receivedSequenceNumber = buffer.getLong(0);
            final long timestamp = buffer.getLong(SIZE_OF_LONG);
            if (receivedSequenceNumber != sequenceNumber) {
                throw new IllegalStateException("Data Loss:" + sequenceNumber + " to " + receivedSequenceNumber);
            }
            final long duration = System.nanoTime() - timestamp;
            histogram.recordValue(duration);
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
        return 1;
    };
    receiveChannel.register(selector, OP_READ, handler);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(histogram, SEND_ADDRESS, buffer, sendChannel, selector, running);
        histogram.reset();
        System.gc();
        LockSupport.parkNanos(1000 * 1000 * 1000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Histogram(org.HdrHistogram.Histogram) IntSupplier(java.util.function.IntSupplier) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Selector(java.nio.channels.Selector)

Example 25 with DatagramChannel

use of java.nio.channels.DatagramChannel in project aeron by real-logic.

the class ReceiveSendUdpPong method main.

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(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel[] receiveChannels = new DatagramChannel[numChannels];
    for (int i = 0; i < receiveChannels.length; i++) {
        receiveChannels[i] = DatagramChannel.open();
        init(receiveChannels[i]);
        receiveChannels[i].bind(new InetSocketAddress("localhost", Common.PING_PORT + i));
    }
    final InetSocketAddress sendAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    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();
        sendChannel.send(buffer, sendAddress);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)214 InetSocketAddress (java.net.InetSocketAddress)92 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)58 MembershipKey (java.nio.channels.MembershipKey)22 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5