Netty4.0 简单学习

/**
 * 这是server的基本格式。基本server都是这样,分四步
 * 1.创建boss和work 线程 这个可以选。我选的是netty标志的支持nio异步的
 * 2.ServerBootstrap group ,创建channel 这里可以选不同的协议的,我选的是tcp/ip协议的,这个可以选
 * 3.加入ServerInitializer 这个是第二部分
 * 4.最后监听 就行了 后面那一长串就是监听监听
 */
public class HelloServer {
	public static void main(String[] args) {

                //epoll 是linux上支持的selector
		//EpollEventLoopGroup boss1 = new EpollEventLoopGroup();
		EventLoopGroup boss = new NioEventLoopGroup();

		EventLoopGroup work = new NioEventLoopGroup(5);

		ServerBootstrap bootstrap = new ServerBootstrap();
		try {	
			bootstrap.group(boss1, work);

			bootstrap.channel(NioServerSocketChannel.class);

			bootstrap.childHandler(new HelloServerInitializer());

			ChannelFuture f = bootstrap.bind(7894).sync().channel().closeFuture().sync();
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally{
			boss.shutdownGracefully();
			work.shutdownGracefully();	
		}
	}
}

2.HelloServerInitializer

**
 *  这是一个中间量,可以相当于一个pipe  一个socket 过来 需要经过各种各种的handler 来编码解码
 *  这就是定义那一个pipeline 然后随便加 后面的socketChannel 是一个TCP/IP socket。
 */
public class HelloServerInitializer extends ChannelInitializer<SocketChannel>{

	@Override
	public void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();

		//pipeline.addLast("Sdecoder", new StringDecoder());
		//pipeline.addLast("Sencoder",new StringEncoder());
        //pipeline 这个方法可以加入很多编码,解码的的类,非常多,这里是对string 和对象的
		pipeline.addLast(new StringEncoder());
		pipeline.addLast(new StringDecoder());
		pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
		pipeline.addLast(new ObjectEncoder());

		//最后加入自己定义的handler 可以加很多
		pipeline.addLast("myHander",new HelloServerHandler());
	}

}

3.HelloServerHandler

public class HelloServerHandler extends ChannelInboundHandlerAdapter{

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("channel up");
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		// TODO Auto-generated method stub
		//读入一个msg
		System.out.println(msg);
		//写出去一个msg
		ctx.write(msg);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		ctx.flush();
	}

客户端代码:

public class Client {
	
	public static void main(String[] args) {

		EventLoopGroup work = new NioEventLoopGroup();
		Bootstrap bootstrap = new Bootstrap();
		try {
		bootstrap.group(work).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

			@Override
			protected void initChannel(SocketChannel ch) throws Exception {
				// TODO Auto-generated method stub
				ch.pipeline().addLast(new ObjectEncoder(),new ObjectDecoder(ClassResolvers.cacheDisabled(null)),new HelloClientHandler());
			}
		});
			bootstrap.connect("127.0.0.1", 7894).sync().channel().closeFuture().sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

这是客户端的client

public class HelloClientHandler extends ChannelInboundHandlerAdapter{

	public final List<String> list = new ArrayList<String>();
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("client channel up");
		
		for(int i=0;i<10;i++){
			list.add(String.valueOf(i)+",");			
		}
		
		ctx.writeAndFlush(list);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		System.out.println(msg);
		ctx.write(msg);
	}

	

猜你喜欢

转载自zhanghaj00.iteye.com/blog/2064456
今日推荐