分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

Netty实现简单UDP服务器

发布时间:2023-09-06 01:35责任编辑:赖小花关键词:暂无标签
本文参考《Netty权威指南》

文件列表:

├── ChineseProverbClientHandler.java├── ChineseProverbClient.java├── ChineseProverbServerHandler.java└── ChineseProverbServer.java
package com.xh.netty.test12;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioDatagramChannel;/** * Created by root on 1/11/18. */public class ChineseProverbServer { ???public void run(int port) { ???????EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); ???????try { ???????????Bootstrap bootstrap = new Bootstrap(); ???????????bootstrap.group(eventLoopGroup).channel(NioDatagramChannel.class) ???????????????????.option(ChannelOption.SO_BROADCAST, true) ???????????????????.handler(new ChineseProverbServerHandler()); ???????????bootstrap.bind(port).sync().channel().closeFuture().sync(); ???????} catch (InterruptedException e) { ???????????e.printStackTrace(); ???????} finally { ???????????eventLoopGroup.shutdownGracefully(); ???????} ???} ???public static void main(String[] args) { ???????ChineseProverbServer server = new ChineseProverbServer(); ???????server.run(8080); ???}}
package com.xh.netty.test12;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.channel.socket.DatagramPacket;import io.netty.util.CharsetUtil;import io.netty.util.internal.ThreadLocalRandom;/** * Created by root on 1/11/18. */public class ChineseProverbServerHandler extends SimpleChannelInboundHandler<DatagramPacket> { ???private static String[] DIRC = {"哈哈哈哈", "呵呵呵", "嘻嘻嘻"}; ???public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { ???????String req = packet.content().toString(CharsetUtil.UTF_8); ???????System.out.println(req); ???????if (req.equalsIgnoreCase("QUERY")) { ???????????ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("RESULT:" + nextQuote(), CharsetUtil.UTF_8), packet.sender())); ???????}else { ???????????ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("RESULT:" + "ERR", CharsetUtil.UTF_8), packet.sender())); ???????} ???} ???private String nextQuote() { ???????int quote = ThreadLocalRandom.current().nextInt(DIRC.length); ???????return DIRC[quote]; ???}}
package com.xh.netty.test12;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.Unpooled;import io.netty.channel.Channel;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.DatagramPacket;import io.netty.channel.socket.nio.NioDatagramChannel;import io.netty.util.CharsetUtil;import java.net.InetSocketAddress;/** * Created by root on 1/11/18. */public class ChineseProverbClient { ???public void run(int port) { ???????EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); ???????Bootstrap bootstrap = new Bootstrap(); ???????try { ???????????bootstrap.group(eventLoopGroup).channel(NioDatagramChannel.class) ???????????????????.option(ChannelOption.SO_BROADCAST, true) ???????????????????.handler(new ChineseProverbClientHandler()); ???????????Channel channel = bootstrap.bind(7070).sync().channel(); ???????????channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QUERY", CharsetUtil.UTF_8), new InetSocketAddress("255.255.255.255", port))).sync(); ???????????if (!channel.closeFuture().await(15000)) { ???????????????System.out.println("out of time"); ???????????} ???????} catch (InterruptedException e) { ???????????e.printStackTrace(); ???????} finally { ???????????eventLoopGroup.shutdownGracefully(); ???????}   ???} ???public static void main(String[] args) { ???????new ChineseProverbClient().run(8080); ???}}
package com.xh.netty.test12;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.channel.socket.DatagramPacket;import io.netty.util.CharsetUtil;/** * Created by root on 1/11/18. */public class ChineseProverbClientHandler extends SimpleChannelInboundHandler<DatagramPacket> { ???protected void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { ???????String result = packet.content().toString(CharsetUtil.UTF_8); ???????System.out.println("client>>>" + result); ???????ctx.close(); ???}}

Netty实现简单UDP服务器

原文地址:https://www.cnblogs.com/lanqie/p/8267498.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved