隨筆-159  評論-114  文章-7  trackbacks-0

          服務(wù)器往外發(fā)數(shù)據(jù)包,不是光一個socketChannel.write(bb); 就完事了


          那是理想狀態(tài)。。

          用我方法才能保證發(fā)出,即使客戶端連接很濫

          public static long flushChannel(SocketChannel socketChannel,ByteBuffer bb, long writeTimeout) throws IOException
           
          {

               SelectionKey key 
          = null;
               Selector writeSelector 
          = null;
               
          int attempts = 0;
               
          int bytesProduced = 0;
               
          try {
                   
          while (bb.hasRemaining()) {
                       
          int len = socketChannel.write(bb);
                       attempts
          ++;
                       
          if (len < 0){
                           
          throw new EOFException();
                       }
           
                       bytesProduced 
          += len;
                       
          if (len == 0{
                           
          if (writeSelector == null){
                               writeSelector 
          = Selector.open();
                               
          if (writeSelector == null){
                                   
          // Continue using the main one
                                   continue;
                               }
           
                           }


                           key 
          = socketChannel.register(writeSelector, key.OP_WRITE);

                           
          if (writeSelector.select(writeTimeout) == 0{

                               
          if (attempts > 2)

                                   
          throw new IOException("Client disconnected");

                           }
           else {

                               attempts
          --;

                           }


                       }
           else {

                           attempts 
          = 0;

                       }


                   }


               }
           finally {

                   
          if (key != null{

                       key.cancel();

                       key 
          = null;

                   }


                   
          if (writeSelector != null{

                       
          // Cancel the key.

                       writeSelector.selectNow();

                       writeSelector.close();

                   }


               }


               
          return bytesProduced;

           }
            


          posted on 2008-11-19 12:08 北國狼人的BloG 閱讀(1450) 評論(1)  編輯  收藏

          評論:
          # re: 用戶NIO寫服務(wù)器,必須多加處理OP_WRITE,不會丟包 2010-11-09 16:14 | raymond
          好用,頂一個!  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 井研县| 西贡区| 兰考县| 沭阳县| 兴化市| 秦安县| 南江县| 乌拉特前旗| 读书| 达孜县| 珲春市| 黔西县| 健康| 襄汾县| 广饶县| 新泰市| 修文县| 祁东县| 蒲城县| 思南县| 尉氏县| 法库县| 淮北市| 凤台县| 平谷区| 贵定县| 修水县| 凌源市| 丽江市| 丽水市| 琼中| 会理县| 锡林郭勒盟| 邢台市| 呈贡县| 大港区| 石楼县| 山东省| 得荣县| 邛崃市| 沙雅县|