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();
            }
        }
    }
  • 相关阅读:
    KVC该机制
    JS多语种方式
    面试经典(1)---翻转字的顺序在一个句子
    正确Linux新手很实用20命令
    代码添加背景音乐的日记
    什么是比特币(Bitcoin)?
    李开复:该算法的重要性
    javascript推断的浏览器类型
    libyuv编
    Linux下将UTF8编码批量转换成GB2312编码的方法
  • 原文地址:https://www.cnblogs.com/qima/p/3117429.html
Copyright © 2011-2022 走看看