Netty系列(二):NettyDemo

本文详细介绍了Netty服务端和客户端的启动流程。服务端涉及NioEventLoopGroup的bossGroup和workerGroup,分别用于监听端口和处理连接。通过ServerBootstrap配置线程组和IO模型,并使用ChannelInitializer定义连接的数据读写和业务处理。客户端启动流程虽未详述,但可推断与服务端类似,涉及连接建立和数据传输的配置。此外,还讨论了handler()、attr()、childAttr()、childOption()和option()等方法在服务端启动过程中的作用,如设置TCP属性和自定义属性。
摘要由CSDN通过智能技术生成

介绍
上篇说到了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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值