<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    夢幻之旅

    DEBUG - 天道酬勤

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
    1.server
    package example.helloword.server;

    import java.net.InetSocketAddress;
    import java.util.concurrent.Executors;

    import org.jboss.netty.bootstrap.ServerBootstrap;
    import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

    import example.helloword.NetConstant;

    public class Server
    {
        
    private static Server server = new Server();
        
        
    private ServerBootstrap bootstrap;
        
        
    private Server()
        
    {}
        
        
    public static Server getInstance()
        
    {
            
    return server;
        }

        
        
    public void start()
        
    {
            bootstrap 
    = new ServerBootstrap(new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(), Executors
                            .newCachedThreadPool()));
            bootstrap.setPipelineFactory(
    new ServerPipelineFactory());
            bootstrap.bind(
    new InetSocketAddress(NetConstant.server_port));
        }

        
        
    public void stop()
        
    {
            bootstrap.releaseExternalResources();
        }

        
        
    public static void main(String[] args)
        
    {
            Server server 
    = Server.getInstance();
            server.start();
        }

    }

    2.ServerPipelineFactory
    package example.helloword.server;

    import static org.jboss.netty.channel.Channels.pipeline;

    import org.jboss.netty.channel.ChannelPipeline;
    import org.jboss.netty.channel.ChannelPipelineFactory;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;

    public class ServerPipelineFactory implements ChannelPipelineFactory
    {
        
    public ChannelPipeline getPipeline() throws Exception
        
    {
            ChannelPipeline pipleline 
    = pipeline();
            pipleline.addLast(
    "encode"new StringEncoder());
            pipleline.addLast(
    "decode"new StringDecoder());
            pipleline.addLast(
    "handler"new ServerHandler());
            
    return pipleline;
        }

    }

    3.handle
    package example.helloword.server;

    import org.jboss.netty.channel.ChannelHandlerContext;
    import org.jboss.netty.channel.ExceptionEvent;
    import org.jboss.netty.channel.MessageEvent;
    import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

    public class ServerHandler extends SimpleChannelUpstreamHandler
    {
        
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
                
    throws Exception
        
    {
            System.out.println(
    "recive message,message content:" + e.getMessage());
            e.getChannel().write(
    "byte");
            
        }

        
        
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
                
    throws Exception
        
    {
            e.getChannel().close();
        }

    }
    clinet:
    package example.helloword.client22;

    import static org.jboss.netty.channel.Channels.pipeline;

    import org.jboss.netty.channel.ChannelPipeline;
    import org.jboss.netty.channel.ChannelPipelineFactory;
    import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
    import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
    import org.jboss.netty.handler.codec.string.StringDecoder;
    import org.jboss.netty.handler.codec.string.StringEncoder;

    public class ClientPipelineFactory implements ChannelPipelineFactory
    {
        
    public ChannelPipeline getPipeline() throws Exception
        
    {
            ChannelPipeline pipleline 
    = pipeline();  
            pipleline.addLast(
    "frameDecoder"new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0404));  
            pipleline.addLast(
    "frameEncode"new LengthFieldPrepender(4false));
            pipleline.addLast(
    "encode"new StringEncoder());  
            pipleline.addLast(
    "decode"new StringDecoder());  
            pipleline.addLast(
    "handler"new ClinetHandler());  
            
    return pipleline;  
        }

    }
    clientpool:
    package example.helloword.client22;

    import java.net.InetSocketAddress;
    import java.util.concurrent.Executors;

    import org.jboss.netty.bootstrap.ClientBootstrap;
    import org.jboss.netty.channel.ChannelFuture;
    import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

    import example.helloword.NetConstant;
    import example.helloword.client2.ClientPipelineFactory;

    public class ClientPool
    {
        
    public static ClientPool clientPool = new ClientPool();
        
        
    private ClientBootstrap bootstrap;
        
        
    private ClientPool()
        
    {
            bootstrap 
    = new ClientBootstrap(new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(), Executors
                            .newCachedThreadPool()));
            
            bootstrap.setPipelineFactory(
    new ClientPipelineFactory());
            bootstrap.setOption(
    "tcpNoDelay"true);
            bootstrap.setOption(
    "keepAlive"true);
        }

        
        
    public static ClientPool getInstance()
        
    {
            
    return clientPool;
        }

        
        
    public void getChannelFuture(String host, int port, String message)
        
    {
            ChannelFuture future 
    = bootstrap.connect(new InetSocketAddress(host,
                    NetConstant.server_port));
            future.awaitUninterruptibly();
            
    if (!future.isSuccess())
            
    {
                future.getCause().printStackTrace();
                future.getChannel().getCloseFuture().awaitUninterruptibly(); 
                
    return;
            }

            future.getChannel().write(message);
        }

        
        
    public static void main(String[] args) throws InterruptedException
        
    {
            
    for (int i = 0; i < 1000; i++)
            
    {
                ClientPool.getInstance().getChannelFuture(
    "127.0.0.1"0,
                        
    "test" + i);
                Thread.sleep(
    1000 * 3);
            }

        }

    }

    posted on 2011-08-30 13:07 HUIKK 閱讀(3419) 評論(2)  編輯  收藏 所屬分類: Javajava Net

    評論

    # re: jboss netty 框架小試 2013-11-04 15:25 kunjie
    LZ,這文章,一點文字說明都沒,沒有多大實用意義  回復  更多評論
      

    # re: jboss netty 框架小試[未登錄] 2014-01-02 14:47 123
    tell us about what you think and code less, plz.  回復  更多評論
      

    主站蜘蛛池模板: 亚洲中文字幕AV每天更新| 亚洲AV人无码激艳猛片| 一区二区亚洲精品精华液| 在线观看免费人成视频| 久久久久se色偷偷亚洲精品av| 亚洲一区二区三区免费视频| 亚洲成a人片7777| 在线观看的免费网站| 亚洲熟妇无码八V在线播放| 搡女人真爽免费视频大全| 亚洲精品乱码久久久久久蜜桃图片 | 亚洲高清国产AV拍精品青青草原| 中文字幕成人免费高清在线| 亚洲精品乱码久久久久久按摩| 最近免费mv在线观看动漫| 亚洲欧洲日产国码久在线观看| 99久在线国内在线播放免费观看| 亚洲国产精品yw在线观看| 真实乱视频国产免费观看| 日本黄页网址在线看免费不卡| 久久久久亚洲爆乳少妇无| 成人片黄网站色大片免费观看APP| 久久精品国产亚洲av成人| 嘿嘿嘿视频免费网站在线观看| 亚洲中文字幕久久精品无码A| 亚洲国产精品不卡毛片a在线| 中文在线日本免费永久18近| 亚洲视频欧洲视频| 国产一级高清视频免费看| 99免费精品视频| 国产成人精品日本亚洲11| 国产成人免费ā片在线观看| 91精品成人免费国产| 亚洲成年人电影网站| 国产jizzjizz免费视频| 久久国产精品免费网站| 亚洲av永久中文无码精品综合 | 最近中文字幕免费mv视频8| 成年大片免费视频播放一级| 亚洲制服中文字幕第一区| 国产成人免费手机在线观看视频|