当前位置: 首页 > news >正文

微网站制作阿里云网站备案入口

微网站制作,阿里云网站备案入口,广告游戏,网页设计与制作课程评价方案使用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);}
http://www.zqtcl.cn/news/797100/

相关文章:

  • 专业的集团网站设计公司优化网站服务
  • 深圳专业网站建设公司好吗个人网站排名欣赏
  • 百度网站流量查询网站建设流程总结
  • 使用代理服务器后看什么网站怎么做动态的实时更新的网站
  • 网站修改titlephp 网站下载器
  • 网站开发飞沐东莞人才市场档案服务中心
  • 北京中小企业网站建设智慧团建官网登录口手机版
  • wordpress插 件seo服务是什么
  • 推荐几个安全没封的网站湖南长大建设集团股份有限公司网站
  • 免费淘宝客网站模板下载怎么申请注册公司
  • 网站动画用什么做wordpress 主题 下载
  • 制作网站的app推动高质量发展的必要性
  • 网站建设培训个人企业的官网
  • 物流公司做网站佛山市城乡和住房建设局网站
  • 建设银行六安市分行网站云梦网络建站
  • 寿光专业做网站的公司有哪些网页制作基础教程黄洪杰
  • discuz可以做门户网站么江西省网站备案
  • 天眼查在线查询系统seo平台优化服务
  • 建设部网站 注册违规北京梵客装饰
  • 大连制作网站报价网站网站怎么做代理
  • php做网站如何架构品牌vi设计欣赏
  • 网站外链建设与文章发布规范网址例子
  • 外贸网站空间选择商业计划书
  • 手机作图软件app专业做邯郸网站优化
  • 济南网站定制制作wordpress theid
  • 企业网站建设能解决什么问题设计房子需要多少钱
  • 专业网站开发制作石家庄信息门户网站定制
  • 藤虎网络广州网站建设网站域名实名认证官网
  • 佛山专业网站建设公司推荐it行业做网站一个月多少钱
  • 三网合一网站怎么做苏醒主题做的网站