介绍
上篇说到了netty,这篇来进阶一下。来介绍下服务端和客户端的启动流程。
服务端启动流程
package com.guo.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.AttributeKey;
public class NettyServer {
private static final int BEGIN_PORT = 8000;
public static void main(String[] args) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
final ServerBootstrap serverBootstrap = new ServerBootstrap();
final AttributeKey<Object> clientKey = AttributeKey.newInstance("clientKey");
serverBootstrap
.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
}).attr(AttributeKey.newInstance("serverName"),"nettyServer")
.childAttr(clientKey,"clientValue")
.option(ChannelOption.SO_BACKLOG,1024)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, tru