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

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

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

    夢幻之旅

    DEBUG - 天道酬勤

       :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      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 閱讀(3407) 評論(2)  編輯  收藏 所屬分類: Javajava Net

    評論

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

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

    主站蜘蛛池模板: 人人狠狠综合久久亚洲88| 暖暖免费在线中文日本| 老外毛片免费视频播放| 久久亚洲精品11p| 国产精品亚洲综合一区在线观看 | 国产综合精品久久亚洲| 亚洲一级Av无码毛片久久精品| 亚洲AV之男人的天堂| 亚洲成a人片在线观看老师| 国产成人亚洲精品影院| 亚洲日产韩国一二三四区| 亚洲AV综合色区无码一区 | 国内自产少妇自拍区免费| 精品国产麻豆免费网站| 免费a在线观看播放| 国产成人精品曰本亚洲79ren| 亚洲精品无码永久中文字幕| 亚洲av无码专区在线播放| 亚洲白色白色在线播放| 亚洲综合色一区二区三区| 久久亚洲精品成人无码| 亚洲第一视频在线观看免费| 成全视频高清免费观看电视剧| 久久这里只精品热免费99| 91九色精品国产免费| 永久免费视频v片www| 亚洲另类少妇17p| 亚洲好看的理论片电影| 亚洲中文字幕久久无码| 一级做a爰片久久毛片免费陪 | 91麻豆精品国产自产在线观看亚洲| 亚洲国产成人片在线观看无码| 久久久久亚洲AV成人片| 亚洲а∨天堂久久精品9966| 国产成人亚洲精品无码AV大片| 黄床大片免费30分钟国产精品| 久久不见久久见免费视频7| 成年私人影院免费视频网站| 亚洲女人被黑人巨大进入| 亚洲国产人成网站在线电影动漫| 亚洲人成综合在线播放|