博客
关于我
Netty4服务端入门代码示例
阅读量:348 次
发布时间: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/

你可能感兴趣的文章
Oracle静默安装
查看>>
【Bert101】变压器模型背后的复杂数学【02/4】
查看>>
Oracle面试题:Oracle中truncate和delete的区别
查看>>
ThreadLocal线程内部存储类
查看>>
thinkphp 常用SQL执行语句总结
查看>>
Oracle:ORA-00911: 无效字符
查看>>
Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
查看>>
Tensorflow.python.framework.errors_impl.ResourceExhaustedError:无法分配内存[操作:AddV2]
查看>>
TCP基本入门-简单认识一下什么是TCP
查看>>
tableviewcell 中使用autolayout自适应高度
查看>>
Symbolic Aggregate approXimation(SAX,符号聚合近似)介绍-ChatGPT4o作答
查看>>
Orcale表被锁
查看>>
svn访问报错500
查看>>
sum(a.YYSR) over (partition by a.hy_dm) 不需要像group by那样需要分组函数。方便。
查看>>
ORCHARD 是什么?
查看>>
Struts2中使用Session的两种方法
查看>>
STM32工作笔记0032---编写跑马灯实验---寄存器版本
查看>>
order by rand()
查看>>
SSM(Spring+SpringMvc+Mybatis)整合开发笔记
查看>>
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>