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

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

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

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

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


    那是理想狀態。。

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

    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 閱讀(1446) 評論(1)  編輯  收藏

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

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲日韩在线中文字幕综合 | 久久亚洲AV成人无码| 又黄又大的激情视频在线观看免费视频社区在线| 免费观看美女用震蛋喷水的视频| 久久亚洲一区二区| 日本免费在线观看| 亚洲一区影音先锋色资源| 99国产精品视频免费观看| 78成人精品电影在线播放日韩精品电影一区亚洲| 青柠影视在线观看免费| 久久综合九九亚洲一区| 亚洲视频在线免费看| 7777久久亚洲中文字幕| 成人片黄网站色大片免费| 久久亚洲精品无码av| 亚洲国产人成中文幕一级二级| 亚洲免费日韩无码系列| 亚洲国产精品无码成人片久久| 免费国产黄网站在线观看可以下载| 亚洲色偷偷av男人的天堂 | 在线播放免费播放av片| 亚洲欧洲日本在线观看| 免费国产人做人视频在线观看| 午夜免费国产体验区免费的| 中文字幕精品亚洲无线码一区| 日本亚洲欧洲免费天堂午夜看片女人员| 亚洲第一精品在线视频| 免费看国产精品3a黄的视频| 国产亚洲精品2021自在线| 在线a亚洲v天堂网2019无码| 最近新韩国日本免费观看| 最新亚洲精品国偷自产在线 | 久久伊人久久亚洲综合| 黄在线观看www免费看| 国产精品亚洲综合| 国产亚洲综合久久系列| 好吊妞在线新免费视频| 一级做a爱片特黄在线观看免费看 一级做a爱过程免费视 | 在线播放国产不卡免费视频| 无码专区—VA亚洲V天堂| 午夜爱爱免费视频|