netty demo
程序员文章站
2022-04-24 11:07:37
...
ReadMe
先运行 NettyServer
再运行 NettyClient
环境
netty 各种版本 pom
https://mvnrepository.com/artifact/io.netty/netty-all
jdk11;
pom
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.67.Final</version>
</dependency>
NettyServer.java
package com.lhh.demo.netty.simple;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class NettyServer {
public static void main(String[] args) throws Exception{
EventLoopGroup boos = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boos,worker)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("客户端上线了:"+ch.remoteAddress());
ch.pipeline().addLast(new NettyServerHandler());
}
});
System.out.println(" server is ready ....");
ChannelFuture sync = bootstrap.bind(9999).sync();
sync.channel().closeFuture().sync();
} catch (Exception e){
e.printStackTrace();
} finally {
worker.shutdownGracefully();
boos.shutdownGracefully();
}
}
}
NettyServerHandler.java
package com.lhh.demo.netty.simple;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.util.concurrent.TimeUnit;
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("ctx="+ctx);
ByteBuf buf = (ByteBuf) msg;
System.out.println("客户端对服务器说:"+buf.toString(CharsetUtil.UTF_8));
System.out.println("接收消息线程:"+Thread.currentThread().getName());
ctx.channel().eventLoop().execute(()->{
try {
System.out.println("task处理消息线程:"+Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(10);
ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端;",CharsetUtil.UTF_8));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("sync execute business..........");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.write(Unpooled.copiedBuffer("hello 客户端;",CharsetUtil.UTF_8));
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
}
NettyClient.java
package com.lhh.demo.netty.simple;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class NettyClient {
public static void main(String[] args) throws Exception{
NioEventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyClientHandler());
}
});
ChannelFuture future = bootstrap.connect("localhost", 9999).sync();
future.channel().closeFuture().sync();
} catch (Exception e){
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
}
NettyClientHandler.java
package com.lhh.demo.netty.simple;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
//客户端链接服务器完成,可以发送数据了
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String str = "hello server";
System.out.println("客户端对服务器说:"+str);
ctx.write(Unpooled.copiedBuffer(str, CharsetUtil.UTF_8));
ctx.flush();
}
//接收到服务器段发送的数据,读取一下
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.out.println("服务器说:"+buf.toString(CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
}
推荐阅读
-
Android编程实现仿优酷圆盘旋转菜单效果的方法详解【附demo源码下载】
-
Python实现使用request模块下载图片demo示例
-
Django框架实现的分页demo示例
-
jQuery实现分页功能(含ajax请求、后台数据、附完整demo)
-
Netty源码分析之ChannelPipeline(二)—ChannelHandler的添加与删除
-
1+X学习日志——导航栏demo
-
vue.js配合$.post从后台获取数据简单demo分享
-
jQuery插件FusionCharts绘制ScrollColumn2D图效果示例【附demo源码下载】
-
基于h5 ajax实现手机定位(demo)
-
art-template的小demo分享(代码分析)