博客
关于我
Netty4服务端入门代码示例
阅读量:348 次
发布时间:2019-03-04

本文共 2743 字,大约阅读时间需要 9 分钟。

??Maven?????Netty???

??????Netty??????Netty???????????????????pom.xml??????????????????Netty??????

?????

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NetttyServer {
public static void main(String[] args) {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer
() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline()
.addLast("encoder", new StringEncoder())
.addLast("decoder", new StringDecoder())
.addLast("hello world handler", new HelloWorldHandler());
}
});
try {
ChannelFuture channelFuture = bootstrap.bind(8080).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
static class HelloWorldHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channel Active and write back......");
String resp = "Hello world";
ChannelFuture future = ctx.channel().writeAndFlush(resp);
future.sync();
System.out.println("success:" + future.isSuccess());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("read msg : " + msg);
String resp = "Hello world";
ctx.channel().writeAndFlush(resp);
}
}
}

?????????Netty????????????????????????

io.netty
netty-all
4.1.36.Final

??????????????????????NioEventLoopGroup??????????NioServerSocketChannel??????????Pipeline????????????????????HelloWorldHandler??????

?????????????????????8080????????????????????????????"Hello world"???????????????????????

???????????????????Netty????????????????

转载地址:http://qume.baihongyu.com/

你可能感兴趣的文章
Numpy如何使用np.umprod重写range函数中i的python
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>
OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
查看>>
object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
查看>>
Objective-C享元模式(Flyweight)
查看>>
Objective-C以递归的方式实现二叉搜索树算法(附完整源码)
查看>>
Objective-C实现1000 位斐波那契数算法(附完整源码)
查看>>
Objective-C实现2 个数字之间的算术几何平均值算法(附完整源码)
查看>>
Objective-C实现3n+1猜想(附完整源码)
查看>>
Objective-C实现A-Star算法(附完整源码)
查看>>
Objective-C实现all combinations所有组合算法(附完整源码)
查看>>
Objective-C实现An Armstrong number阿姆斯特朗数算法(附完整源码)
查看>>
Objective-C实现anagrams字谜算法(附完整源码)
查看>>
Objective-C实现ApproximationMonteCarlo蒙特卡洛方法计算pi值算法 (附完整源码)
查看>>
Objective-C实现area under curve曲线下面积算法(附完整源码)
查看>>
Objective-C实现armstrong numbers阿姆斯壮数算法(附完整源码)
查看>>
Objective-C实现atoi函数功能(附完整源码)
查看>>
Objective-C实现average mean平均数算法(附完整源码)
查看>>