netty案例
来源:互联网 发布:activiti engine源码 编辑:程序博客网 时间:2024/06/14 20:48
jar包
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.3.0.Final</version>
</dependency>
======================================================================================
服务端
package com.huishao;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
class ServerHanlder extends SimpleChannelHandler {
// 通道被关闭的时候被触发
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
// TODO Auto-generated method stub
super.channelClosed(ctx, e);
System.out.println("channelClosed");
}
// 建立连接、通道被关闭的时候才会触发
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
// TODO Auto-generated method stub
super.channelDisconnected(ctx, e);
System.out.println("channelDisconnected");
}
// 接收异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(ctx, e);
System.out.println("exceptionCaught");
}
// 接受客户端数据
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
// TODO Auto-generated method stub
super.messageReceived(ctx, e);
System.out.println("messageReceived");
System.out.println("服务器获取客户端发送来的参数:" + e.getMessage());
ctx.getChannel().write("你好啊!");
}
}
// 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("serverHanlder", new ServerHanlder());
return pipeline;
}
});
//绑定端口号
Channel channel = serverBootstrap.bind(new InetSocketAddress(8080));
System.out.println("服务器端已经被启动...");
}
}
======================================================================================
客户端
package com.huishao;
import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
class ClientHanlder extends SimpleChannelHandler{
//通道被关闭的时候会触发
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
// TODO Auto-generated method stub
super.channelClosed(ctx, e);
System.out.println("channelClosed");
}
//必须建立连接,通道被关闭的时候被触发
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
// TODO Auto-generated method stub
super.channelDisconnected(ctx, e);
System.out.println("channelDisconnected");
}
//接收 异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
// TODO Auto-generated method stub
super.exceptionCaught(ctx, e);
System.out.println("exceptionCaught");
}
//接收客户端的数据
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
// TODO Auto-generated method stub
super.messageReceived(ctx, e);
System.out.println("messageReceived");
System.out.println("服务端向客户端回复的内容:"+e.getMessage());
}
}
public class NettyClinet {
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("clientHanlder", new ClientHanlder());
return pipeline;
}
});
//5.绑定端口号
ChannelFuture connect = clientBootstrap.connect(new InetSocketAddress("127.0.0.1", 8080));
Channel channel = connect.getChannel();
System.out.println("客户端已经被启动...");
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请输入内容");
channel.write(scanner.next());
}
}
}
- netty案例
- netty案例
- netty案例
- netty小案例demo
- Netty传输服务案例
- Netty入门案例
- netty 入门案例hello world
- Netty之——基于Netty5.0高级案例NettyWebsocket
- 基于Netty5.0中级案例一之Netty与Web
- Java网络编程之Netty入门案例-yellowcong
- Netty
- Netty
- Netty
- Netty
- Netty
- Netty
- netty
- netty
- Array属性和方法
- python中 对文件的读写操作 以及如何边写入 边保存flush()
- method
- idea激活码,留备用
- Android Recycleview的用法
- netty案例
- Java之美[从菜鸟到高手演练]之atomic包的原理及分析
- 解决jnlp安全警告框
- 2017.12.20
- python基本语法
- js获取元素相对于父级元素的高度
- AndroidStudio的相关设置(一)
- RS485收发的3种典型电路-重点-自动收发电路
- SQL UPDATE(修改)