什么是ServerBootstrap
-
是Netty的启动引导类,作用是注入所需要的参数
-
设置Channel类型, BIO 或 OIO等,每个Channel就是一个TCP连接
-
初始化子通道childHandler,即接收一个连接后,对这个连接的处理
-
配置option参数,作用于每新建一个通道Channel。设置TCP连接中的一些参数,比如常用的有
==ChannelOption.SO_BACKLOG: ==存放【已完成】的三次握手请求的队列最大长度,如果SO_BACKLOG配置比SOMAXCONN大,则会以SOMAXCONN为准,所以SO_BACKLOG要小于SOMAXCONN,是TCP全连接队列数==ChannelOption.TCP_NODELAY: ==解决Nagle算法问题,即TCP每次发送的请求包大小,默认为false,即TPC请求包到来后累计到一定大小后一起发送。如果设置为true,则是追求高实时性,有数据立刻发送
-
remoteAddress(客户端的参数): 配置要连接的服务端ip,port, 与服务端进行连接
服务端源码:
import io.netty.bootstrap.ServerBootstrap;
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.NioServerSocketChannel;
public class EchoServer {
private int port;
public EchoServer(int port){
this.port = port;
}
public void run() throws InterruptedException {
// 配置服务端线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
// 收到客户端内容,并处理逻辑在这里
// 直接使用EchoServerHandler自定义类的处理逻辑
socketChannel.pipeline().addLast(new EchoServerHandler());
}
});
System.out.println("服务端启动ing");
// 绑定端口,同步等待成功
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
// 等待服务端监听端口关闭
channelFuture.channel().closeFuture().sync();
}finally {
// 关闭线程组
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws InterruptedException {
int port = 8080;
if(args != null && args.length > 0){
port = Integer.parseInt(args[0]);
}
new EchoServer(port).run();
}
}
客户端源码:
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;
import java.net.InetSocketAddress;
public class EchoClient {
private String host;
private Integer port;
public EchoClient(String host, Integer port){
this.host = host;
this.port = port;
}
public void start() throws InterruptedException {
EventLoopGroup eventExecutors = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventExecutors)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoClientHandler());
}
});
System.out.println("服务端启动ing");
// 绑定端口,同步等待成功
ChannelFuture channelFuture = bootstrap.connect().sync();
// 等待服务端监听端口关闭
channelFuture.channel().closeFuture().sync();
}finally {
// 关闭线程组
eventExecutors.shutdownGracefully();
}
}
public static void main(String[] args) throws InterruptedException {
new EchoClient("127.0.0.1", 8080).start();
}
}