1.创建maven项目并引入依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
2.编写服务端代码
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @author 冯铁城 [17615007230@163.com]
* @date 2022-05-09 19:11:57
* @describe: 时间服务器服务端
*/
public class TimeServer {
public static void main(String[] args) {
int port = 8080;
if(null != args && 0 < args.length){
port = Integer.parseInt(args[0]);
}
new TimeServer().bind(port);
}
public void bind(int port) {
//1.创建线程组用于接受服务端链接
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
//2.创建线程组用户socketChannel读写
NioEventLoopGroup workGroup = new NioEventLoopGroup();
try {
//3.创建服务端
ServerBootstrap server = new ServerBootstrap();
server.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChildChannelHandler());
//3.绑定端口,开启同步等
ChannelFuture sync = server.bind(port).sync();
System.out.println("服务端已启动!");
//4.等待服务端监听端口关闭
sync.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new TimeServerHandler());
}
}
}
- 创建用于接收客户端链接线程组、负责socketIO的线程组
- 开启Netty服务端
- 服务端配置线程组、渠道、配置TCP参数、定义IO事件处理类
- 同步绑定端口开启
- 最终优雅退出线程
- IO时间处理类添加具体IO处理类
3.编写具体IO处理类
```java import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext;
import java.util.Scanner;
/**
- @author 冯铁城 [17615007230@163.com]
- @date 2022-05-09 20:41:33
@describe: 时间服务器服务端 */ public class TimeServerHandler extends ChannelHandlerAdapter {
private static final Scanner SCANNER = new Scanner(System.in);
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("服务端已与" + ctx.channel().id() + "建立链接");
}
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//1.读取消息为字节数组
ByteBuf buf = (ByteBuf) msg;
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
//2.字符串判定
String body = new String(bytes, "UTF-8");
System.out.println("收到" + ctx.channel().id() + "渠道的请求:" + body);
//3.定义返回结果
System.out.println("请输入响应信息");
String responseMessage = SCANNER.nextLine();
//4.相应数据
ByteBuf response = Unpooled.copiedBuffer(responseMessage.getBytes());
ctx.write(response);
}
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
} } ```
- 继承ChannelHandlerAdapter类
- 重写channelActive方法,当客户端或服务端建立链接时,触发此方法
- 重写channelRead方法,当客户端