一、概念

Netty學習路線總結
netty是nio架構。
任何nio架構做通信,一定是server先啟動,然後,client端調用connect方法。
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;
public class Server {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(); //一個用于處理伺服器端接收用戶端連接配接的
EventLoopGroup workGroup = new NioEventLoopGroup(); //一個是進行網絡通信的(網絡讀寫的)
ServerBootstrap b = new ServerBootstrap(); //2建立輔助工具類,用于伺服器通道的一系列配置
b.group(bossGroup, workGroup) //綁定兩個線程組
.channel(NioServerSocketChannel.class) //指定NIO的模式
.option(ChannelOption.SO_BACKLOG, 1024) //設定tcp緩沖區
.option(ChannelOption.SO_SNDBUF, 32 * 1024) //設定發送緩沖大小
.option(ChannelOption.SO_RCVBUF, 32 * 1024) //這是接收緩沖大小
.option(ChannelOption.SO_KEEPALIVE, true) //保持連接配接
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ServerHanlder()); //3.在這裡配置具體資料接收方法的處理
}
});
ChannelFuture cf1 = b.bind(6666).sync(); //4.進行綁定
cf1.channel().closeFuture().sync(); //5.等待關閉
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ServerHanlder extends ChannelHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req, "utf-8");
System.out.println("Server: " + body);
String response = "888";//服務端給用戶端的響應
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
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 Client {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture cf1 = b.connect("127.0.0.1", 6666).sync();
cf1.channel().write(Unpooled.copiedBuffer("777".getBytes())); //用戶端發出資料
cf1.channel().flush();
cf1.channel().closeFuture().sync();
group.shutdownGracefully();
}
}
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
public class ClientHandler extends ChannelHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
ByteBuf buf = (ByteBuf) msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req, "utf-8");
System.out.println("Client: " + body);
} finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
如何斷開用戶端的連接配接?
開啟多個端口