我是靠谱客的博主 鲜艳面包,这篇文章主要介绍netty 入门案例hello world,现在分享给大家,希望可以做个参考。

1.依赖的jar包

复制代码
1
2
3
4
5
6
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.8.Final</version> </dependency>

2. 服务端代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class NettyServer { public void start(int port) throws Exception { ServerBootstrap strap = new ServerBootstrap(); EventLoopGroup boosGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { strap.group(boosGroup, workerGroup). channel(NioServerSocketChannel.class). option(ChannelOption.SO_BACKLOG, 1024). childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyServerHandler()); } }); ChannelFuture future=strap.bind(port).sync(); future.channel().closeFuture().sync(); }finally { boosGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { System.out.println("start server"); new NettyServer().start(8000); } }

NettyServerHandler

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class NettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf=(ByteBuf)msg; byte [] data=new byte[buf.readableBytes()]; buf.readBytes(data); System.out.println(new String(data)); String responseMsg="收到"+System.getProperty("line.separator"); ByteBuf sendMsg= Unpooled.copiedBuffer(responseMsg.getBytes()); ctx.writeAndFlush(sendMsg); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { super.channelReadComplete(ctx); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); } }

启动服务后,可以使用telnet 127.0.0.1 8000 来测试

3.客户端代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class NettyClient { public void connect(int port,String host) throws Exception { EventLoopGroup eventLoopGroup=new NioEventLoopGroup(); Bootstrap bootstrap=new Bootstrap(); try { bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyClientHandler()); } }); ChannelFuture future = bootstrap.connect(host, port).sync(); future.channel().closeFuture().sync(); }finally { eventLoopGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception{ new NettyClient().connect(8000,"127.0.0.1"); } }

NettyClientHandler

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class NettyClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("hello world".getBytes()); ctx.writeAndFlush(buf); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf=(ByteBuf)msg; byte[] data=new byte[buf.readableBytes()]; buf.readBytes(data); System.out.print(new String(data)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); } }

最后

以上就是鲜艳面包最近收集整理的关于netty 入门案例hello world的全部内容,更多相关netty内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部