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

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

??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/

你可能感兴趣的文章
pip 下载慢
查看>>
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>
Pip 安装挂起
查看>>
pip 或 pip3 为 Python 3 安装包?
查看>>
pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
查看>>
pip 无法从 requirements.txt 安装软件包
查看>>
pip/pip3更换国内源
查看>>
pip3 install PyQt5 --user 失败
查看>>
pip3命令全解析:Python3包管理工具的详细使用指南
查看>>
pip3安装命令重复创建文件‘/tmp/pip-install-xxxxx/package‘失败
查看>>
PIPE 接口信号列表
查看>>
pipeline配置与管理Job企业级实战
查看>>
pipeline项目配置实战
查看>>
Pipenv 与 Conda?
查看>>
QVGA/HVGA/WVGA/FWVGA分辨率屏含义及大小//Android虚拟机分辨率
查看>>
pipreqs : 无法将“pipreqs”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径 正确,然后再试一次。
查看>>
pipy国内镜像的网址
查看>>