zoukankan      html  css  js  c++  java
  • [javanetty] 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();
            }
        }
    }
  • 相关阅读:
    SpringBoot EnumValidator验证器实现
    【原创】SpringCloud:基于Spring Cloud netflix全家桶搭建一个完整的微服务架构系统
    Hystrix Dashboard监控报“Unable to connect to Command Metric Stream”?
    Mysql sql_mode的合理设置
    nginx 调优
    函数指针
    进程与线程
    大小端学习
    联合体和结构体
    内存分配
  • 原文地址:https://www.cnblogs.com/qima/p/3117429.html
Copyright © 2011-2022 走看看