Netty中客户端、服务端的编解码器 作为服务端而言: 主要工作就是接收客户端请求,将客户端的请求内容解码;发送响应给客户端,并将发送内容编码 所以,服务端需要两个编解码器
- HttpRequestDecoder(将请求内容解码)
- HttpResponseEncoder(将响应内容编码)
作为客户端而言: 主要工作就是发送请求给服务端,并将发送内容编码;接收服务端响应,并将接收内容解码; 所以,客户端需要两个编解码器
- HttpResponseDecoder(将响应内容解码)
- HttpRequestEncoder(将请求内容编码)
这次我们可以编写服务端和客户端,将二者连接起来,实现双向的通信。
大致的套路和逻辑还和以前相似,新建下面这几个类
public class NettyServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverGroup = new ServerBootstrap();
serverGroup.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new NettyInitializer());
ChannelFuture channelFuture = serverGroup.bind(8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
public class NettyInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
channelPipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
channelPipeline.addLast(new LengthFieldPrepender(4));
channelPipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
channelPipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
channelPipeline.addLast(new NettyHandler());
}
}
public class NettyHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(ctx.channel().remoteAddress()+" "+msg);
ctx.channel().writeAndFlush("from server "+ UUID.randomUUID());
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("from server data");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
.handler(new NettyClientInitializer2());
ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}
public class NettyClientInitializer2 extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
channelPipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
channelPipeline.addLast(new LengthFieldPrepender(4));
channelPipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
channelPipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
channelPipeline.addLast(new NettyClientHandler());
}
}
public class NettyClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(ctx.channel().remoteAddress());
System.out.println(msg);
ctx.channel().writeAndFlush(""+System.currentTimeMillis());
// ctx.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
之后启动server和client就可以看到控制台里打印的信息了。