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));
        }
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值