package io.netty.handler.codec.mytest;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
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 java.net.InetSocketAddress;
public class MyServerTest {
static int PORT = 8899;
public static void main(String[] args) throws InterruptedException {
EventLoopGroup elg = new NioEventLoopGroup(2);
ServerBootstrap sb = new ServerBootstrap();
sb.group(elg);
sb.channel(NioServerSocketChannel.class); // 设置 NIO 类型的 Channel
sb.localAddress(new InetSocketAddress(PORT)); // 设置监听端口
// 装配流水线
sb.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel sc) { // 有连接到达时就会创建 Channel
sc.pipeline().addLast("toStr", new StringDecoder()); // 接收时用解码器
sc.pipeline().addLast("test", new TestHandler()); // sign_demo_010
}
});
ChannelFuture cf = sb.bind().sync(); // sign_demo_001
System.out.println("启动完成 -------------");
cf.channel().closeFuture().sync();
System.out.println("关闭完成 -------------");
elg.shutdownGracefully();
}
static class TestHandler extends ChannelInboundHandlerAdapter {
@Override // sign_demo_020
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
new RuntimeException("栈跟踪-处理器被添加").printStackTrace();
super.handlerAdded(ctx);
}
@Override // sign_demo_021
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
new RuntimeException("栈跟踪-处理器被注册").printStackTrace();
super.channelRegistered(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("读取消息:" + msg);
super.channelRead(ctx, msg);
}
}
}
package io.netty.handler.codec.mytest;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import java.time.LocalTime;
public class MyClientTest {
public static void main(String[] args) {
EventLoopGroup elg = new NioEventLoopGroup(1);
Bootstrap cb = new Bootstrap();
cb.group(elg);
cb.channel(NioSocketChannel.class);
cb.remoteAddress("localhost", MyServerTest.PORT);
// 设置通道初始化
cb.handler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast("toByte", new StringEncoder()); // 发送时用编码器
}
});
System.out.println("客户端开始连接...");
ChannelFuture cf = cb.connect(); // 异步发起连接
cf.addListener((ChannelFuture f) -> {
if (f.isSuccess()) {
System.out.println("连接成功!");
String msg = "中-Test 123..." + LocalTime.now();
Channel channel = f.channel();
channel.write(msg); // sign_u_001 写入消息
System.out.println("发送消息:" + msg);
channel.flush(); // sign_u_002 推送消息
channel.close().sync(); // 关闭 channel
elg.shutdownGracefully(); // 关闭线程池(这样才会退出)
} else {
System.out.println("连接还未成功!");
}
});
}
}