下载资源后端资源详情
boot-example-netty-tcp-2.0.5.zip
大小:29.03KB
价格:10积分
下载量:0
评分:
5.0
上传者:myyhtw
更新日期:2025-09-22

boot-example-netty-tcp-2.0.5

资源文件列表(大概)

文件名
大小
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/pom.xml
1.74KB
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/
-
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/controller/
-
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/java/boot/example/tcp/client/netty/
-
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
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
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
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
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
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
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
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/resources/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/main/resources/application.properties
24B
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/tcp/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-client-2.0.5/src/test/java/boot/example/tcp/client/
-
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/pom.xml
1.74KB
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/
-
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/controller/
-
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/java/boot/example/tcp/server/netty/
-
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
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
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
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
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
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
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/resources/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/main/resources/application.properties
24B
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/tcp/
-
boot-example-netty-tcp-2.0.5/boot-example-base-tcp-server-2.0.5/src/test/java/boot/example/tcp/server/
-
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

资源内容介绍

使用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.Sharablepublic 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); } }

用户评论 (0)

发表评论

captcha