微网站制作,阿里云网站备案入口,广告游戏,网页设计与制作课程评价方案使用NettySpringBoot方式可以快速地开发一套基于UDP协议的服务端程序#xff0c;同样的也可以开发客户端#xff0c;一般使用UDP都是使用原生的方式#xff0c;发送消息后就不管不问#xff0c;也就是不需要确定消息是否收到#xff0c;这里使用Netty创建的客户端和服务端…使用NettySpringBoot方式可以快速地开发一套基于UDP协议的服务端程序同样的也可以开发客户端一般使用UDP都是使用原生的方式发送消息后就不管不问也就是不需要确定消息是否收到这里使用Netty创建的客户端和服务端倒是能够类似http协议那样请求数据得到返回数据实际上得到的就是服务端原路返回的数据。
1、这里也使用SpringBootNetty创建pom.xml文件导入依赖包
propertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.version/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.1.6.RELEASE/versionrelativePath / !-- lookup parent from repository --/parentdependencies!--web模块的启动器 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- netty依赖 springboot2.x自动导入版本 --dependencygroupIdio.netty/groupIdartifactIdnetty-all/artifactId/dependency/dependencies
2、Netty客户端的类包含main方法这里就没有使用SpringBoot方式的启动
package boot.netty.udp.client;import java.net.InetSocketAddress;
import boot.netty.udp.client.adapter.BootNettyUdpClientSimpleChannelInboundHandler;
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;public class BootNettyUdpClient {public void bind(String address, int port, String data) {EventLoopGroup eventLoopGroup new NioEventLoopGroup();try {Bootstrap clientBootstrap new Bootstrap();clientBootstrap clientBootstrap.group(eventLoopGroup);clientBootstrap clientBootstrap.channel(NioDatagramChannel.class);clientBootstrap clientBootstrap.option(ChannelOption.SO_BROADCAST, true);clientBootstrap clientBootstrap.handler(new BootNettyUdpClientSimpleChannelInboundHandler());Channel channel clientBootstrap.bind(0).sync().channel();channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(data, CharsetUtil.UTF_8),new InetSocketAddress(address,port))).sync();// 与BootNettyUdpClientSimpleChannelInboundHandler中的ctx.channel().id().toString()是一样的值System.out.println(channnel id channel.id().toString());// 方式一查询等待超时 单位s 等待服务端原路返回的消息// 在channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)方法中// 收到消息后可主动关闭channel此处等待自然释放 channel.closeFuture().await(10000);// 方式二直接等待服务端原路返回后在channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)方法中// 收到消息后可主动关闭channe// 若服务端没有原路返回消息或者消息未收到将会一直等待浪费资源//channel.closeFuture().sync();} catch (Exception e) {// TODO: handle exception} finally {System.out.println(netty client udp close!);eventLoopGroup.shutdownGracefully();}}public static void main(String[] args) {// 向网段内的所有机器广播UDP消息这个没试过是不是这个原理// new BootNettyUdpClient().bind(255.255.255.255,9999,I am client);// 指定某个套接字地址和发送的内容可以发送消息// 该方式也可以封装成一个udp的客户端的send类new BootNettyUdpClient().bind(127.0.0.1,9999,I am client);}}
3、服务端I/O数据读写处理类
package boot.netty.udp.client.adapter;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;public class BootNettyUdpClientSimpleChannelInboundHandler extends SimpleChannelInboundHandlerDatagramPacket {Overrideprotected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {try {String strdata msg.content().toString(CharsetUtil.UTF_8);//打印收到的消息System.out.println(msg---strdata);// 与BootNettyUdpClient中的channel.id().toString()是一样的值System.out.println(ctx.channel().id().toString());// 收到服务端原路返回的消息后不需要再次向服务端发送消息, 可以在这里暴力关闭也可以在 channelReadComplete(ChannelHandlerContext ctx)内// ctx.close();} catch (Exception e) {}}/*** 重写方法* 结构* 1.public class BootNettyUdpClientSimpleChannelInboundHandler extends SimpleChannelInboundHandlerDatagramPacket** 2.public abstract class SimpleChannelInboundHandlerI extends ChannelInboundHandlerAdapter** 3.public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter implements ChannelInboundHandler** ChannelInboundHandlerAdapter类有诸多方法可以重写可以根据具体需求来写**/Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {super.channelReadComplete(ctx);System.out.println(关闭channel);ctx.close();}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}}4、我们可以启动一个服务端(SpringBoot搭建基于UDP协议的服务端)然后调用main方法可以看到服务端可以收到消息客户端也可以收到服务端原路返回的时间戳消息public static void main(String[] args) {// 向网段内的所有机器广播UDP消息这个没试过是不是这个原理// new BootNettyUdpClient().bind(255.255.255.255,9999,I am client);// 指定某个套接字地址和发送的内容可以发送消息// 该方式也可以封装成一个udp的客户端的send类new BootNettyUdpClient().bind(127.0.0.1,9999,I am client);}
4、我们可以启动一个服务端(SpringBoot搭建基于UDP协议的服务端)然后调用main方法可以看到服务端可以收到消息客户端也可以收到服务端原路返回的时间戳消息
public static void main(String[] args) {// 向网段内的所有机器广播UDP消息这个没试过是不是这个原理// new BootNettyUdpClient().bind(255.255.255.255,9999,I am client);// 指定某个套接字地址和发送的内容可以发送消息// 该方式也可以封装成一个udp的客户端的send类new BootNettyUdpClient().bind(127.0.0.1,9999,I am client);}