欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 基于 Netty 框架的 Java TCP 服务器端实现,用于启动一个 TCP 服务器来处理客户端的连接和数据传输

基于 Netty 框架的 Java TCP 服务器端实现,用于启动一个 TCP 服务器来处理客户端的连接和数据传输

2025/5/7 13:54:12 来源:https://blog.csdn.net/weixin_38223427/article/details/147013953  浏览:    关键词:基于 Netty 框架的 Java TCP 服务器端实现,用于启动一个 TCP 服务器来处理客户端的连接和数据传输

代码:

package com.example.tpson_tcp;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;/*** Netty服务端类,用于启动TCP服务器。*/
@Service
public class NettyServer {/*** 在对象初始化后自动调用的方法,用于启动服务器。* 无参数和返回值。*/@PostConstructpublic void main() {// 创建EventLoopGroup用于处理网络事件EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();try {// 配置服务器启动参数ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // 指定使用的NIO通道类.childHandler(new ChannelInitializer<SocketChannel>() { // 设置通道初始化处理器@Overrideprotected void initChannel(SocketChannel ch) throws Exception {// 在通道中添加处理链,用于解码、编码和处理设备数据ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new DeviceDataHandler());}}).option(ChannelOption.SO_BACKLOG, 128) // 设置连接队列大小.childOption(ChannelOption.SO_KEEPALIVE, true); // 启用TCP KeepAlive// 绑定端口并启动服务器ChannelFuture channelFuture = serverBootstrap.bind(8092).sync();// 等待服务器关闭channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {// 异常处理,抛出运行时异常throw new RuntimeException(e);} finally {// 关闭EventLoopGroup,释放资源workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}
}

代码解释:

1. 包导入部分

java

package com.example.tpson_tcp;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;
  • package com.example.tpson_tcp;:指定该类所属的包名。
  • 后续一系列 import 语句引入了 Netty 框架的相关类,用于网络编程。ServerBootstrap 用于配置和启动服务器;EventLoopGroup 是用于处理网络事件的线程池;StringDecoder 和 StringEncoder 用于对字符串进行编解码。
  • org.springframework.stereotype.Service 表明这是一个 Spring 服务类。
  • javax.annotation.PostConstruct 用于标记一个方法,该方法会在对象初始化后自动调用。

2. 类定义和注解

java

  • @Service:这是 Spring 框架的注解,将 NettyServer 类标记为一个服务组件,Spring 会自动扫描并管理这个类的实例。
  • public class NettyServer:定义了一个名为 NettyServer 的公共类。

3. main 方法

java

@PostConstruct
public void main() {
  • @PostConstruct:该注解表明 main 方法会在 NettyServer 类的实例被创建并初始化后自动调用。
  • public void main():定义了一个公共的无返回值方法 main

4. 创建 EventLoopGroup

java

EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

  • NioEventLoopGroup 是 Netty 提供的基于 Java NIO 的事件循环组,用于处理网络事件。
  • bossGroup 主要负责接受客户端的连接请求。
  • workerGroup 负责处理已经建立的连接的读写操作。

5. 配置服务器启动参数

java

ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new DeviceDataHandler());}}).option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); 

  • ServerBootstrap 用于配置和启动服务器。
  • group(bossGroup, workerGroup):指定使用的 EventLoopGroup
  • channel(NioServerSocketChannel.class):指定使用的 NIO 通道类,用于接受客户端的连接。
  • childHandler:设置通道初始化处理器,当有新的连接建立时,会调用 initChannel 方法。在该方法中,向通道的处理链中添加了 StringDecoderStringEncoder 和 DeviceDataHandlerStringDecoder 用于将接收到的字节数据解码为字符串,StringEncoder 用于将字符串编码为字节数据发送出去,DeviceDataHandler 用于处理具体的业务逻辑。
  • option(ChannelOption.SO_BACKLOG, 128):设置连接队列的大小,即允许等待处理的最大连接数。
  • childOption(ChannelOption.SO_KEEPALIVE, true):启用 TCP 的 KeepAlive 机制,用于检测长时间空闲的连接是否仍然有效。

6. 绑定端口并启动服务器

java

ChannelFuture channelFuture = serverBootstrap.bind(8092).sync();
channelFuture.channel().closeFuture().sync();
  • serverBootstrap.bind(8092):将服务器绑定到指定的端口 8092。
  • sync() 方法是一个同步方法,会阻塞当前线程,直到绑定操作完成。
  • channelFuture.channel().closeFuture().sync():等待服务器关闭,会阻塞当前线程,直到服务器的通道关闭。

7. 异常处理和资源释放

java

} catch (InterruptedException e) {throw new RuntimeException(e);
} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();
}
  • catch (InterruptedException e):捕获 InterruptedException 异常,当线程在等待过程中被中断时会抛出该异常,将其包装成 RuntimeException 重新抛出。
  • finally 块:无论是否发生异常,都会执行该块中的代码,调用 shutdownGracefully() 方法来优雅地关闭 workerGroup 和 bossGroup,释放资源。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词