https://www.bilibili.com/video/BV1jK4y1s7GV?p=47 --- client server 端代码调试
https://www.bilibili.com/video/BV1jK4y1s7GV?p=46 --- client 端代码编写及讲解
https://www.bilibili.com/video/BV1jK4y1s7GV?p=45 --- server端代码编写及讲解
https://www.bilibili.com/video/BV1jK4y1s7GV?p=44 --- server端代码编写及讲解
package com.tz.leo.netty.simple;
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.channel.socket.nio.NioSocketChannel;
public class NettyServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyServerHandler());
}
})
;
System.out.println(" step 01 服务器端启动 ok ... ");
ChannelFuture cf = bootstrap.bind(8097).sync();
cf.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package com.tz.leo.netty.simple;
import com.sun.org.apache.xpath.internal.SourceTree;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.util.CharsetUtil;
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("=================step04=================");
System.out.println(" 服务器读取线程:"+ Thread.currentThread().getName());
System.out.println(" server ctx = "+ ctx);
System.out.println(" ====== show channel pipeline ======");
Channel channel = ctx.channel();
ChannelPipeline pipeline = ctx.pipeline();
System.out.println();
ByteBuf buf=(ByteBuf)msg;
System.out.println(" 客户端发送消息是: " + buf.toString(CharsetUtil.UTF_8));
System.out.println(" 客户端 地址是: "+ ctx.channel().remoteAddress());
System.out.println("=================step04=================");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer(" step05 hello, 客户端 ~ 今天任总被判刑了,我略有心酸... ",CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
package com.tz.leo.netty.simple;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class NettyClient {
public static void main(String[] args) throws Exception{
EventLoopGroup clientGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(clientGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyClientHandler());
}
});
System.out.println(" step 02 客户端启动 ok ...");
ChannelFuture cf = bootstrap.connect("127.0.0.1", 8097).sync();
cf.channel().closeFuture().sync();
} finally {
clientGroup.shutdownGracefully();
}
}
}
package com.tz.leo.netty.simple;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("============ step03 ==========");
ctx.writeAndFlush(Unpooled.copiedBuffer(" hello, 服务端 ~ 我是苟声 ", CharsetUtil.UTF_8));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("=================step06=================");
ByteBuf buf =(ByteBuf)msg;
System.out.println("服务器回复的消息是: " + buf.toString(CharsetUtil.UTF_8));
System.out.println("服务端的地址是"+ ctx.channel().remoteAddress());
System.out.println("=================step06=================");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("step07 finish ");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}