首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

boot-example-netty-tcp-2.0.5

后端 29.03KB 17 需要积分: 1
立即下载

资源介绍:

使用SpringBoot2.x集成Netty4.x创建基于TCP/IP协议的服务端和客户端的Demo代码案例 使用了SpringBoot+swaggerUI方式进行测试,客户端可以给服务端发送消息,服务端也可以给已经连上的客户端发送消息,使用了通道保活机制,可以使用demo搭建基于tcp协议的物联网平台
package boot.example.tcp.server.netty; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; /** * I/O数据读写处理类 * 蚂蚁舞 */ @ChannelHandler.Sharable public class BootNettyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter{ /** * 注册时执行 */ @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { super.channelRegistered(ctx); System.out.println("--channelRegistered--"+ctx.channel().id().toString()); } /** * 离线时执行 */ @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); System.out.println("--channelUnregistered--"+ctx.channel().id().toString()); } /** * 从客户端收到新的数据时,这个方法会在收到消息时被调用 */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { try { if(msg == null){return;} String data = (String) msg; data = data.replaceAll("\r|\n", ""); String channelId = ctx.channel().id().toString(); System.out.println("channelId="+channelId + "data="+data); // 这里我将通道id作为code来使用,实际是需要msg里来摘取的客户端数据里的唯一值的 // 如果没有则创建 如果有,更新data值 BootNettyChannel b = BootNettyChannelCache.get("server:"+channelId); if(b == null){ BootNettyChannel bootNettyChannel = new BootNettyChannel(); bootNettyChannel.setChannel(ctx.channel()); bootNettyChannel.setCode("server:"+channelId); bootNettyChannel.setReport_last_data(data); BootNettyChannelCache.save("server:"+channelId, bootNettyChannel); } else { b.setReport_last_data(data); } ctx.writeAndFlush(Unpooled.buffer().writeBytes(("server:"+channelId).getBytes())); // netty的编码已经指定,因此可以不需要再次确认编码 // ctx.writeAndFlush(Unpooled.buffer().writeBytes(channelId.getBytes(CharsetUtil.UTF_8))); } catch (Exception e) { System.out.println("channelRead--"+e.toString()); } } /** * 从客户端收到新的数据、读取完成时调用 */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws IOException { System.out.println("channelReadComplete"); ctx.flush(); } /** * 当出现 Throwable 对象才会被调用,即当 Netty 由于 IO 错误或者处理器在处理事件时抛出的异常时 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws IOException { System.out.println("exceptionCaught"); cause.printStackTrace(); BootNettyChannel bootNettyChannel = BootNettyChannelCache.get("server:"+ctx.channel().id().toString()); if(bootNettyChannel != null){ BootNettyChannelCache.remove("server:"+ctx.channel().id().toString()); } ctx.close();//抛出异常,断开与客户端的连接 } /** * 客户端与服务端第一次建立连接时 执行 */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception, IOException { super.channelActive(ctx); ctx.channel().read(); InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress(); String clientIp = inSocket.getAddress().getHostAddress(); //此处不能使用ctx.close(),否则客户端始终无法与服务端建立连接 System.out.println("channelActive:"+clientIp+ctx.name()); } /** * 客户端与服务端 断连时 执行 */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception, IOException { super.channelInactive(ctx); InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress(); String clientIp = inSocket.getAddress().getHostAddress(); System.out.println("channelInactive:"+clientIp); BootNettyChannel bootNettyChannel = BootNettyChannelCache.get("server:"+ctx.channel().id().toString()); if(bootNettyChannel != null){ BootNettyChannelCache.remove("server:"+ctx.channel().id().toString()); } ctx.close(); //断开连接时,必须关闭,否则造成资源浪费,并发量很大情况下可能造成宕机 } /** * 服务端当read超时, 会调用这个方法 */ @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception, IOException { super.userEventTriggered(ctx, evt); InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress(); String clientIp = inSocket.getAddress().getHostAddress(); ctx.close();//超时时断开连接 System.out.println("userEventTriggered:"+clientIp); } }

资源文件列表:

boot-example-netty-tcp-2.0.5.zip 大约有58个文件
  1. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/
  2. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/pom.xml 1.74KB
  3. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/
  4. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/
  5. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/
  6. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/
  7. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/
  8. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/
  9. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/
  10. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/BootNettyClientApplication.java 1.18KB
  11. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/controller/
  12. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/controller/BootNettyClientController.java 2.15KB
  13. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/
  14. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyChannelInboundHandlerAdapter.java 2.73KB
  15. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyChannelInitializer.java 876B
  16. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyClient.java 1.94KB
  17. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyClientChannel.java 716B
  18. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyClientChannelCache.java 835B
  19. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyClientThread.java 475B
  20. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/BootNettyHeartTimer.java 963B
  21. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/SwaggerConfig.java 1.42KB
  22. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/resources/
  23. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/resources/application.properties 24B
  24. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/
  25. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/
  26. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/
  27. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/
  28. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/tcp/
  29. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/tcp/client/
  30. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/tcp/client/BootNettyClientApplicationTest.java 130B
  31. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/
  32. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/pom.xml 1.74KB
  33. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/
  34. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/
  35. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/
  36. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/
  37. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/
  38. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/
  39. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/
  40. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/BootNettyServerApplication.java 923B
  41. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/controller/
  42. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/controller/BootNettyController.java 2.61KB
  43. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/
  44. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/BootNettyChannel.java 763B
  45. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/BootNettyChannelCache.java 799B
  46. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/BootNettyChannelInboundHandlerAdapter.java 5.38KB
  47. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/BootNettyChannelInitializer.java 1.44KB
  48. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/BootNettyServer.java 3.95KB
  49. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/SwaggerConfig.java 1.42KB
  50. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/resources/
  51. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/resources/application.properties 24B
  52. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/
  53. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/
  54. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/
  55. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/
  56. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/tcp/
  57. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/tcp/server/
  58. boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/tcp/server/BootNettyServerApplicationTest.java 128B
0评论
提交 加载更多评论
其他资源 win32汇编资料大全
win32汇编资料大全
记事本程序源码
java写的记事本程序源码
Bamboy高斯模糊(毛玻璃)
目前性能最高的毛玻璃实现方式,简单轻松实现高斯模糊
AceAdmin1.3.2_中英双语完整版(2015-5-21_update)
ACE后台管理界面模板是基于bootstrap写的一款后台管理界面html全站模板
ShareSDKv2.1.1 简化压缩 使用教程
ShareSDKv2.1.1 简化压缩 使用教程 blog: http://blog.csdn.net/li6185377/article/details/8956171
php-5.2.8-Win32.zip
php-5.2.8-Win32.zip PHP
Sybase学习笔记.zip
Sybase学习笔记.zip
改进版的yolov5+双目测距
新版本代码特点:(注意目前只适用于2560*720分辨率的双目,其他分辨率需要修改) 1、替换“回”字形查找改为“米”字形查找,可以设置存储像素点的个数20可修改,然后取有效像素点的中位数(个人觉得比平均值更有代表性)。 2、每10帧(约1/3秒)双目匹配一次,提升代码的运行速度。 3、可以进行实时检测,运行速度与机器的性能有关。
改进版的yolov5+双目测距 改进版的yolov5+双目测距 改进版的yolov5+双目测距