[java-netty] netty学习记录

这是一个系列,这是第一篇http://www.coderli.com/netty-course-hello-world

下面是我的源码:--服务端

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
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.Channels;
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;

/**
 * Netty 服务端代码 * 
 */
public class HelloServer {

    public static void main(String args[]) {
        // Server服务启动器
        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(Executors
                        .newCachedThreadPool(), Executors.newCachedThreadPool()));
        // 设置一个处理客户端消息和各种消息事件的类(Handler)
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new HelloServerHandler());
            }
        });
        // 开放8000端口供客户端访问。
        bootstrap.bind(new InetSocketAddress(8000));
    }

    private static class HelloServerHandler extends SimpleChannelHandler {

        /**
         * 当有客户端连接上来时:        
         */
        @Override
        public void channelConnected(ChannelHandlerContext ctx,
                ChannelStateEvent e) {
            System.out.println("收到连接:"+e.getChannel().getRemoteAddress().toString());
        }

        public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
            System.out.println("连接断开:"+e.getChannel().getRemoteAddress().toString());
        }

        /**
         * 接受客户端发来的消息,在有客户端消息到达时触发
         */
        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
            System.out.println("收到连接【"+e.getChannel().getRemoteAddress().toString()+"】发来数据");
            ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
            e.getChannel().write(e.getMessage());//原样消息发送回去
            
            // System.out.println(buffer.toString(Charset.defaultCharset()));
            // 五位读取
            while (buffer.readableBytes() >= 5) {
                ChannelBuffer tempBuffer = buffer.readBytes(5);            
                System.out.println(tempBuffer.toString(Charset.defaultCharset()));
            }
            // 读取剩下的信息
            System.out.println(buffer.toString(Charset.defaultCharset()));
        }
        
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
            System.out.println("接收【"+e.getChannel().getRemoteAddress().toString()+"】异常:"+ e.getCause());
            e.getChannel().close();
        }
    }
}

 

posted @ 2013-06-04 17:05  祺祺怪怪的  阅读(1279)  评论(0编辑  收藏  举报