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();
            }
        }
    }
  • 相关阅读:
    Django 常见问题
    post和get的区别
    Django 基础学习笔记二
    Django 中的分页器
    Python 微服务框架 Nameko 微服务通信(RabbitMQ)
    《大数据白皮书 2020.12》解读
    练习Div+Css
    利用JAVAScript调用WebService
    统计在线人数和历史访问人数
    自己写的一个DBHelper
  • 原文地址:https://www.cnblogs.com/qima/p/3117429.html
Copyright © 2011-2022 走看看