夢幻之旅

          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 閱讀(3411) 評論(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.  回復  更多評論
            

          主站蜘蛛池模板: 竹山县| 通榆县| 宁波市| 阿图什市| 建始县| 安新县| 柘城县| 遵义县| 南宫市| 霍山县| 建始县| 莱州市| 上虞市| 通许县| 沂水县| 芜湖县| 乐清市| 策勒县| 黄平县| 扬中市| 沙河市| 丹棱县| 哈尔滨市| 大方县| 马鞍山市| 南投市| 沾化县| 绥江县| 虎林市| 始兴县| 门源| 宁海县| 咸丰县| 邛崃市| 涞水县| 界首市| 大悟县| 岳阳市| 平邑县| 丹阳市| 突泉县|