一、引入jar
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.3.0.Final</version>
</dependency>
二、服务端案例代码
class ServerHandler extends SimpleChannelHandler {
// 通道被关闭的时候会触发
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelClosed(ctx, e);
System.out.println("channelClosed");
}
// 必须要建立连接,关闭通道的时候才会触发
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
System.out.println("channelDisconnected");
}
// 接受出现异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
System.out.println("exceptionCaught");
}
// 接受客户端数据..
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
super.messageReceived(ctx, e);
System.out.println("messageReceived");
System.out.println("服务器获取客户端发来的参数:" + e.getMessage());
ctx.getChannel().write("你好啊!");
}
}
/**
* @program: zhq_netty
* @description:
* @author: HQ Zheng
* @create: 2019-09-25 15:43
*/
// netty 服务器端
public class NettyServer {
public static void main(String[] args) {
// 1.创建服务对象
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 2.创建两个线程池 第一个 监听端口号 nio监听
ExecutorService boos = Executors.newCachedThreadPool();
ExecutorService wook = Executors.newCachedThreadPool();
// 3.将线程池放入工程
serverBootstrap.setFactory(new NioServerSocketChannelFactory(boos, wook));
// 4.设置管道工程
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
// 设置管道
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = org.jboss.netty.channel.Channels.pipeline();
// 传输数据的时候直接为string类型
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// 设置事件监听类
pipeline.addLast("serverHandler", new ServerHandler());
return pipeline;
}
});
// 绑定端口号
serverBootstrap.bind(new InetSocketAddress(8080));
System.out.println("服务器端已经被启动.....");
}
}
测试是否正常运行
访问:
三、客户端代码
class ClientHandler extends SimpleChannelHandler{
// 通道被关闭的时候会触发
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelClosed(ctx, e);
System.out.println("channelClosed");
}
// 必须要建立连接,关闭通道的时候才会触发
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
System.out.println("channelDisconnected");
}
// 接受出现异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
System.out.println("exceptionCaught");
}
// 接受客户端数据..
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
super.messageReceived(ctx, e);
System.out.println("messageReceived");
System.out.println("服务器向客户端回复的内容:" + e.getMessage());
}
}
/**
* @program: zhq_netty
* @description: netty客户端
* @author: HQ Zheng
* @create: 2019-09-25 15:53
*/
public class NettyClient {
public static void main(String[] args) {
// 1.创建服务对象
ClientBootstrap clientBootstrap = new ClientBootstrap();
// 2.创建两个线程池 第一个 监听端口号 nio监听
ExecutorService boos = Executors.newCachedThreadPool();
ExecutorService wook = Executors.newCachedThreadPool();
// 3.将线程池放入工程
clientBootstrap.setFactory(new NioClientSocketChannelFactory(boos, wook));
// 4.设置管道工程
clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
// 设置管道
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = org.jboss.netty.channel.Channels.pipeline();
// 传输数据的时候直接为string类型
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// 设置事件监听类
pipeline.addLast("clientHandler", new ClientHandler());
return pipeline;
}
});
// 绑定端口号
ChannelFuture connect = clientBootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));
Channel channel = connect.getChannel();
System.out.println("client start");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入内容:");
channel.write(scanner.next());
}
}
}
四、测试验证