Example usage for io.netty.channel.socket DatagramPacket DatagramPacket

List of usage examples for io.netty.channel.socket DatagramPacket DatagramPacket

Introduction

In this page you can find the example usage for io.netty.channel.socket DatagramPacket DatagramPacket.

Prototype

public DatagramPacket(ByteBuf data, InetSocketAddress recipient) 

Source Link

Document

Create a new instance with the specified packet data and recipient address.

Usage

From source file:books.netty.protocol.udp.ChineseProverbClient.java

License:Apache License

public void run(int port) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//  ww w. j a  va  2 s  .co  m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChineseProverbClientHandler());
        Channel ch = b.bind(0).sync().channel();
        // ?UDP?
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("?", CharsetUtil.UTF_8),
                new InetSocketAddress("255.255.255.255", port))).sync();
        if (!ch.closeFuture().await(15000)) {
            System.out.println("!");
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:books.netty.protocol.udp.ChineseProverbServerHandler.java

License:Apache License

public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) {
    String req = packet.content().toString(CharsetUtil.UTF_8);
    System.out.println(req);/*from w ww .j a  v a  2 s  .c  om*/
    if ("?".equals(req)) {
        ctx.writeAndFlush(new DatagramPacket(
                Unpooled.copiedBuffer(": " + nextQuote(), CharsetUtil.UTF_8),
                packet.sender()));
    }
}

From source file:c5db.codec.UdpProtostuffEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, UdpProtostuffMessage<T> msg, List<Object> out)
        throws Exception {
    LinkBuffer buffer = new LinkBuffer(bufferAllocSize);
    if (protostuffOutput) {
        LowCopyProtostuffOutput lcpo = new LowCopyProtostuffOutput(buffer);
        schema.writeTo(lcpo, msg.message);
    } else {/*  w  w  w. j  ava 2s . com*/
        LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(buffer);
        schema.writeTo(lcpo, msg.message);
    }

    List<ByteBuffer> buffers = buffer.finish();
    ByteBuf data = Unpooled.wrappedBuffer(buffers.toArray(new ByteBuffer[buffers.size()]));
    data.retain();

    DatagramPacket dg = new DatagramPacket(data, msg.remoteAddress);
    dg.retain();
    out.add(dg);
}

From source file:cl.uandes.so.server.LoginServerHandler.java

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    //if(packet.content().readableBytes() < 24) {
    //    return;
    //}//from   ww  w. jav a 2 s. c  om
    // TEST Cliente: echo -e "\xff\xff\xff\xff\xff\xff\xff\xff\x41\x67\x65\x74\x41\x64\x64\x72\x65\x73\x73\x26\x50\x6f\x72\x74" | nc -4 -w 1 -u 127.0.0.1 9990
    String expected_payload = "ffffffffffffffff416765744164647265737326506f7274";
    //System.err.println(packet);
    //System.out.println(packet.toString());

    byte[] payload = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0x41, (byte) 0x67, (byte) 0x65, (byte) 0x74, (byte) 0x41,
            (byte) 0x64, (byte) 0x64, (byte) 0x72, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x26,
            (byte) 0x50, (byte) 0x6f, (byte) 0x72, (byte) 0x74 };

    ByteBuf data = packet.content().readBytes(packet.content().readableBytes());
    System.out.println(data.array().length);

    byte[] header = { (byte) 0xff, (byte) 0x52 };

    System.out.println(Utils.byteArray2Hex(data.array()));
    if (Utils.byteArray2Hex(data.array()).equals(Utils.byteArray2Hex(payload))) {
        System.out.println("Got login of client, sending multicast group address (header+multicastgroup)");
        ctx.write(new DatagramPacket(
                Unpooled.copiedBuffer(ArrayUtils.addAll(header, multicastgroup.getBytes())), packet.sender()));
    }

}

From source file:cn.pengj.udpdemo.EchoSeverHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    // ??/*w  w w .  j  av a 2s. c o  m*/
    ByteBuf buf = (ByteBuf) packet.copy().content();
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req, CharsetUtil.UTF_8);
    System.out.println("?NOTE>>>>>> ?" + body);

    // ???
    ctx.writeAndFlush(new DatagramPacket(
            Unpooled.copiedBuffer("HelloServer" + System.currentTimeMillis(),
                    CharsetUtil.UTF_8),
            packet.sender())).sync();
}

From source file:co.rsk.net.discovery.UDPChannel.java

License:Open Source License

void sendPacket(byte[] wire, InetSocketAddress address) {
    DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address);
    channel.write(packet);/*from w w  w  .  j  a v a2s .  c  o  m*/
    channel.flush();
}

From source file:com.andrewkroh.cicso.rtp.NettyRtpSession.java

License:Apache License

/**
 * {@inheritDoc}//from www  .j  a v a2  s  . c  om
 *
 * @throws IllegalStateException
 *             if {@code shutdown} has already been called
 */
@Override
public void sendData(RtpPacket rtpPacket) {
    Preconditions.checkNotNull(rtpPacket);
    checkNotShutdown();

    for (Destination destination : destinations) {
        ByteBuf buffer = Unpooled.copiedBuffer(rtpPacket.getBytes());
        channel.writeAndFlush(new DatagramPacket(buffer, destination.getSocketAddress()));
    }
}

From source file:com.changxx.phei.netty.protocol.udp.ChineseProverbServerHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    String req = packet.content().toString(CharsetUtil.UTF_8);
    System.out.println(req);//from w ww.  j  av  a2 s  .c o m
    if ("?".equals(req)) {
        ctx.writeAndFlush(new DatagramPacket(
                Unpooled.copiedBuffer(": " + nextQuote(), CharsetUtil.UTF_8),
                packet.sender()));
    }
}

From source file:com.flysoloing.learning.network.netty.qotm.QuoteOfTheMomentClient.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from ww w  .ja  v a2  s .  c  o m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new QuoteOfTheMomentClientHandler());

        Channel ch = b.bind(0).sync().channel();

        // Broadcast the QOTM request to port 8080.
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
                SocketUtils.socketAddress("255.255.255.255", PORT))).sync();

        // QuoteOfTheMomentClientHandler will close the DatagramChannel when a
        // response is received.  If the channel is not closed within 5 seconds,
        // print an error message and quit.
        if (!ch.closeFuture().await(5000)) {
            System.err.println("QOTM request timed out.");
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.qotm.QuoteOfTheMomentServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    System.err.println(packet);//from  w  ww  .j  a v a  2s . com
    if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) {
        ctx.write(new DatagramPacket(Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8),
                packet.sender()));
    }
}