Netty (code-demo)

简介: Netty code

Netty

#服务端

#客户端
EventLoop
EventLoopServer
EventLoopClient

Java

@Slf4j

publicclassEventLoopClient{

publicstaticvoidmain(String[]args)throwsInterruptedException{

// 1.启动类

Channelchannel=newBootstrap()

// 2.添加EventLoop

.group(newNioEventLoopGroup())

// 3.选择客户端channel实现

.channel(NioSocketChannel.class)

// 4.添加处理器

.handler(newChannelInitializer<NioSocketChannel>(){

                   @Override//连接初始化调用

protectedvoidinitChannel(NioSocketChannelch)throwsException{

ch.pipeline().addLast(newStringEncoder());

}

//5.链接到服务器

})

.connect(newInetSocketAddress("localhost",8080))

.sync()

.channel();

System.out.println(channel);

System.out.println("");

}

}

SO_BACKLOG

ByteBuf 源码

Java

// 池化技术

static{

StringallocType=SystemPropertyUtil.get(

"io.netty.allocator.type",PlatformDependent.isAndroid()?"unpooled" : "pooled");

allocType=allocType.toLowerCase(Locale.US).trim();


ByteBufAllocatoralloc;

if("unpooled".equals(allocType)){

alloc=UnpooledByteBufAllocator.DEFAULT;

logger.debug("-Dio.netty.allocator.type: {}",allocType);

}elseif("pooled".equals(allocType)){

alloc=PooledByteBufAllocator.DEFAULT;

logger.debug("-Dio.netty.allocator.type: {}",allocType);

}else{

alloc=PooledByteBufAllocator.DEFAULT;

logger.debug("-Dio.netty.allocator.type: pooled (unknown: {})",allocType);

}


DEFAULT_ALLOCATOR=alloc;

//是否使用堆内存  默认采用直接内存

// We should always prefer direct buffers by default if we can use a Cleaner to release direct buffers.

DIRECT_BUFFER_PREFERRED=CLEANER!=NOOP

&&!SystemPropertyUtil.getBoolean("io.netty.noPreferDirect",false);

if(logger.isDebugEnabled()){

logger.debug("-Dio.netty.noPreferDirect: {}",!DIRECT_BUFFER_PREFERRED);

}

目录
相关文章
|
Dubbo 前端开发 Java
Failed to bind NettyServer on ×××,cause: io/netty/bootstrap/ServerBootstrap
Failed to bind NettyServer on ×××,cause: io/netty/bootstrap/ServerBootstrap
Failed to bind NettyServer on ×××,cause: io/netty/bootstrap/ServerBootstrap
|
12月前
编译netty报错缺少 io.netty.collection 包 解决方案
编译netty报错缺少 io.netty.collection 包 解决方案
36 0
|
安全
Netty入门Demo
Netty入门Demo
Netty入门Demo
Netty(一)之helloworld
Netty(一)之helloworld
58 0
SpringBoot项目中使用request.getInputStream()获取参数时出现java.io.IOException: Stream closed
@RequestBody注解会先调用request.getInputStream()这个方法获取参数,然而request.getInputStream()这个流只能被读取一次,所以我们再次读取这个流就会报错。 解决方案: 最简单的方案就是 先读取流,然后在将流写进去就行了
netty之helloworld
netty网络编程的学习
Netty - java.lang.NoSuchMethodError:io.netty.bootstrap.Bootstrap.channel
Netty - java.lang.NoSuchMethodError:io.netty.bootstrap.Bootstrap.channel
168 0
Netty - java.lang.NoSuchMethodError:io.netty.bootstrap.Bootstrap.channel
|
Java 程序员 数据安全/隐私保护
netty系列之:JVM中的Reference count原来netty中也有
netty系列之:JVM中的Reference count原来netty中也有
|
网络协议 前端开发
Netty: WebSocket应用,代码demo
Netty: WebSocket应用,代码demo
Netty: WebSocket应用,代码demo

热门文章

最新文章