netty实现UDP单播

提问者

/**
 * 提问端
 */
public class UdpQuestionSide {

    public final static String QUESTION = "请你告诉我一句古诗";

    private static final EventLoopGroup group = new NioEventLoopGroup();

    private static final Bootstrap bs = new Bootstrap();

    public static void start() throws InterruptedException {
        try {
            bs.group(group)
                    .handler(new QuestoinHandler())
                    //UDP的channel
                    .channel(NioDatagramChannel.class);

            //不需要建立连接,绑定0端口是表示系统为我们设置端口监听
            Channel channel = bs.bind(0).sync().channel();

            //UDP使用DatagramPacket发送数据
            channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(QUESTION, CharsetUtil.UTF_8),
                    new InetSocketAddress("127.0.0.1", 8080)));

            //15秒后未获取响应就打印超时
            if (!channel.closeFuture().await(15000)) {
                System.out.println("查询超时!");
            }

        } finally {
            group.shutdownGracefully();
        }
    }


    public static void main(String[] args) throws InterruptedException {
        UdpQuestionSide.start();
    }
}

提问者处理器

/**
 * 提问端处理器
 */
public class QuestoinHandler extends SimpleChannelInboundHandler<DatagramPacket> {


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {

        //获取内容
        String content = datagramPacket.content().toString(CharsetUtil.UTF_8);
        System.out.println(content);
        channelHandlerContext.close();
    }
}

应答者

/**
 * 应答者
 */
public class UdpAnswerSide {

    private static final EventLoopGroup group = new NioEventLoopGroup();

    private static final Bootstrap bs = new Bootstrap();

    public static void start() throws InterruptedException {
        try {
            bs.group(group)
                    .handler(new AnswerHandler())
                    //UDP的channel
                    .channel(NioDatagramChannel.class);

            //没有接受客户端连接的过程,监听本地端口即可
            Channel channel = bs.bind(8080).sync().channel();

            System.out.println("应答服务已启动.....");
            channel.closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }


    public static void main(String[] args) throws InterruptedException {
        UdpAnswerSide.start();
    }


}

应答者处理器

/**
 * 应答者处理器
 */
public class AnswerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    String answer = "只要功夫深,铁棒磨成针。" +
            "旧时王谢堂前燕,飞入寻常百姓家。";

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
        String content = datagramPacket.content().toString(CharsetUtil.UTF_8);
        if (UdpQuestionSide.QUESTION.equals(content)) {
            //提问者的地址
            InetSocketAddress address = datagramPacket.sender();

            channelHandlerContext.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(answer, CharsetUtil.UTF_8),
                    address));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28822933/article/details/83661445