<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

    flash 的socket 真不錯,什么事都內部給辦了,換了java,有些麻煩。

    網上那些nio的例子簡單到無實際價值。

    ByteBuffer totalReceiveBuff = ByteBuffer.allocate(30000);

    ..


    private void newParseSocketData(int remainSize,SelectionKey key) throws IOException {
            
    if (remainSize == 0)
                totalReceiveBuff.clear();
            
    int firstReadSize = remainSize;
            
    while(true)
            
    {
                
    if(key.isReadable())
                
    {
                    firstReadSize 
    += sc.read(totalReceiveBuff);
                    
    break;
                }

                
    try {
                    Thread.sleep(
    200);
                }
     catch (InterruptedException e) {
                    
    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            
    if (firstReadSize < 8{
                System.out.println(firstReadSize 
    + "sssss");
                
    return;
            }

            totalReceiveBuff.flip();
            
    if (totalReceiveBuff.getInt() != 開始標識) {
                
    return;
            }


            
    int msgLength = totalReceiveBuff.getInt();

            
    if (totalReceiveBuff.remaining() < msgLength + 4{
                
    int hasReadSize = firstReadSize;
                
    int totalSize = msgLength + 12;
                totalReceiveBuff.position(totalReceiveBuff.limit());
                totalReceiveBuff.limit(totalReceiveBuff.capacity());
                
    while (true{
                    
    int currentLoopReadSize = sc.read(totalReceiveBuff);
                    hasReadSize 
    += currentLoopReadSize;
                    
    if (hasReadSize >= totalSize) {
                        
    break;
                    }
     else {
                        
    continue;
                    }

                }

                totalReceiveBuff.flip();
                
    if (totalReceiveBuff.getInt() != 開始標識) {
                    
    return;
                }

                
    int size = totalReceiveBuff.getInt();
                
    byte[] bytes = new byte[size];
                totalReceiveBuff.get(bytes);
                
    if (totalReceiveBuff.getInt() != 結束標識) {
                    
    return;
                }

                ByteBuffer realContentBuff 
    = ByteBuffer.wrap(bytes);
                
    int contentRealSize = realContentBuff.getShort();

                
    byte[] stringBytes = new byte[contentRealSize];
                realContentBuff.get(stringBytes);
                handleServerCommand(
    new String(stringBytes, "UTF-8"));
            }
     else {
                
    byte[] bytes = new byte[msgLength];
                totalReceiveBuff.get(bytes);
                
    if (totalReceiveBuff.getInt() != 結束標識) {
                    
    return;
                }

                ByteBuffer realContentBuff 
    = ByteBuffer.wrap(bytes);
                
    int contentRealSize = realContentBuff.getShort();

                
    byte[] stringBytes = new byte[contentRealSize];
                realContentBuff.get(stringBytes);
                handleServerCommand(
    new String(stringBytes, "UTF-8"));
            }


            
    // handle remain need to read
            if (totalReceiveBuff.hasRemaining()) {
                
    byte[] remainBytes = new byte[totalReceiveBuff.remaining()];
                totalReceiveBuff.get(remainBytes);
                totalReceiveBuff.clear();
                totalReceiveBuff.put(remainBytes);

                newParseSocketData(remainBytes.length,key);
            }
     else {
                totalReceiveBuff.clear();
                
    if(key.isReadable())
                
    {
                    
    int pendingcounter = 0;
                    
    int available = 0;
                    
    while((pendingcounter++ < 20))
                    
    {                    
                        available 
    = sc.read(totalReceiveBuff);
                        
    if (available <= 0)
                        
    {
                            
    try {
                                Thread.sleep(
    100);
                            }
     catch (InterruptedException e) {
                                
    // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            
    continue;
                        }

                        
    else
                        
    {
                            newParseSocketData(available,key);
                            
    break;
                        }

                    }

                    
                }

            }


        }


    在網絡不好的情況,其實你無法保證一次讀到的信息就是你要的那個正好的大小,你需要對于數據包進行積累,或者多讀了,需要繼續處理下次從socket讀到的剩余信息。

    而flash 比較簡單

    public function parseSocketData()
            
    {
                
    if(this.socketEchoSize==0){
                    
    if (socket.bytesAvailable < 8return;
                    
    //包頭標志
                    if(socket.readInt()!=開始標識){
                        var bytes:ByteArray
    =new ByteArray();
                        socket.readBytes(bytes,
    0,socket.bytesAvailable);
                        
    this.socketEchoSize=0;
                        
    return;
                    }

                    
    this.socketEchoSize=socket.readInt();
                }

                
    //trace(socket.bytesAvailable, this.socketEchoSize);
                
    //如果長度不夠就攢下來
                if (socket.bytesAvailable < this.socketEchoSize + 4)
                
    {
                    
    //socket.readShort();
                    
    //trace("data lost in transport:\n", socket.readUTFBytes(socket.bytesAvailable));
                    return;
                }

                var buffer : ByteArray 
    = new ByteArray();
                socket.readBytes(buffer, 
    0this.socketEchoSize);
                
    //包尾標志
                if (socket.readInt() == 結束標識)
                
    {
                    var sx:String 
    = buffer.readUTFBytes(buffer.readShort());
                    
    //trace("received:\n" + sx);
                    this.receive(sx);
                }

                
    this.socketEchoSize=0;
                
    if(socket.bytesAvailable>0){
                    
    this.parseSocketData();
                }

            }

    人家flash的socket自己下面就幫你不斷的讀著,你每次之需要判斷byteAvailable屬性大于0否,就ok了。

    java  ,嚴謹。都需要在byteBuffer上面坐判斷。




    最正確的寫法:

    private void newParseSocketData() throws IOException {
            
    int count = 1;
            
    int byteRead = 0;

            Selector readSelector 
    = null;
            SelectionKey tmpKey 
    = null;

            
    try{
                
    while (count > 0{
                    count 
    = sc.read(totalReceiveBuff); // [1]
                    if (count > -1)
                    
    {
                        byteRead 
    += count;
                        
                    }
                        
                }


                
    if (byteRead == 0{
                    readSelector 
    = Selector.open();
                    count 
    = 1;
                    
                    tmpKey 
    = sc.register(readSelector, SelectionKey.OP_READ);
        
                    tmpKey.interestOps(tmpKey.interestOps() 
    | SelectionKey.OP_READ);
        
                    
    int code = readSelector.select(200); // [3]
        
                    tmpKey.interestOps(
        
                    tmpKey.interestOps() 
    & (~SelectionKey.OP_READ));
        
                    
    if (code == 0{
        
                        
    return// Return on the main Selector and try again.
        
                    }

        
                    
    while (count > 0 ) {
        
                        count 
    = sc.read(totalReceiveBuff); // [4]
        
                        
    if (count > -1)
                        
    {
                            byteRead 
    += count;
                        }

                        
                    }

        
                }

            }
    finally
            
    {
                
    if (tmpKey != null)

                    tmpKey.cancel();

                
    if (readSelector != null){

                    
    try{
                        readSelector.selectNow();

                    }
     catch (IOException ex){
                        ;
                    }

                    readSelector.close();
                }

            }


            totalReceiveBuff.flip();
            
            
    while (totalReceiveBuff.remaining() > 8{

                
    if (totalReceiveBuff.getInt() != 592464711{
                    
    return;
                }

                
    int size = totalReceiveBuff.getInt();
                
    if (totalReceiveBuff.remaining() < size + 4{
                    
    int hasReadSize = byteRead;
                    
    int totalSize = size + 12;
                    totalReceiveBuff.position(totalReceiveBuff.limit());
                    totalReceiveBuff.limit(totalReceiveBuff.capacity());
                    
    while (true{
                        
    int currentLoopReadSize = sc.read(totalReceiveBuff);
                        hasReadSize 
    += currentLoopReadSize;
                        
    if (hasReadSize >= totalSize) {
                            
    break;
                        }
     else {
                            
    continue;
                        }

                    }

                    totalReceiveBuff.flip();
                    
    if (totalReceiveBuff.getInt() != 592464711{
                        
    return;
                    }

                    
    int msgLength = totalReceiveBuff.getInt();
                    
    byte[] bytes = new byte[msgLength];
                    totalReceiveBuff.get(bytes);
                    
    if (totalReceiveBuff.getInt() != 1347110691{
                        
    return;
                    }

                    ByteBuffer realContentBuff 
    = ByteBuffer.wrap(bytes);
                    
    int contentRealSize = realContentBuff.getShort();

                    
    byte[] stringBytes = new byte[contentRealSize];
                    realContentBuff.get(stringBytes);
                    handleServerCommand(
    new String(stringBytes, "UTF-8"));
                }

                
    else
                
    {
        
                    
    byte[] bytes = new byte[size];
                    totalReceiveBuff.get(bytes);
                    
    if (totalReceiveBuff.getInt() != 1347110691{
                        
    return;
                    }

                    ByteBuffer realContentBuff 
    = ByteBuffer.wrap(bytes);
                    
    int contentRealSize = realContentBuff.getShort();
        
                    
    byte[] stringBytes = new byte[contentRealSize];
                    realContentBuff.get(stringBytes);
                    handleServerCommand(
    new String(stringBytes, "UTF-8"));
                }


            }


            totalReceiveBuff.clear();    

        }






    posted on 2008-11-04 15:14 北國狼人的BloG 閱讀(460) 評論(1)  編輯  收藏

    評論:
    # re: 如何使用NIO 分次讀取信息 攢包 2009-02-11 13:59 | zkp
    我想問,FLash的Socket也是NIO嗎?  回復  更多評論
      

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


    網站導航:
     
    主站蜘蛛池模板: 在线播放亚洲第一字幕| 春暖花开亚洲性无区一区二区| 亚洲A∨精品一区二区三区| 亚洲一区二区免费视频| 久久国产一片免费观看| 日韩亚洲翔田千里在线| 在线a亚洲老鸭窝天堂av高清| 日批视频网址免费观看| 欧美日韩亚洲精品| 亚洲毛片基地4455ww| 91大神亚洲影视在线| 亚洲AV无码一区二区三区DV| 亚洲中文字幕无码爆乳av中文| 成年女人毛片免费视频| 国产高清不卡免费在线| 日本在线免费观看| 免费播放在线日本感人片| 成人免费网站视频www| 日日摸日日碰夜夜爽亚洲| 亚洲欧美中文日韩视频| 色偷偷女男人的天堂亚洲网| 亚洲成AV人片久久| 亚洲精品偷拍无码不卡av| 久久久久亚洲av无码专区导航| 亚洲国产第一站精品蜜芽| 久久亚洲精品成人综合| 国产亚洲精品资源在线26u| 亚洲乱色熟女一区二区三区丝袜| 亚洲国产成人影院播放| 国内精品久久久久久久亚洲| 免费一级黄色毛片| 亚洲精品国产V片在线观看 | 亚洲熟妇无码久久精品| 久久精品国产亚洲AV香蕉| 色噜噜综合亚洲av中文无码| 久久亚洲精品中文字幕| 亚洲人成日本在线观看| 亚洲依依成人亚洲社区| 亚洲AV永久无码精品一福利| 国产成人+综合亚洲+天堂| 羞羞视频在线观看免费|