2011-10-21 9 views
0

thisプロトコルを使用して通信しようとしています。ソケットが多くのデータを返す以外は正常に動作します。今、パケットが\ r \ nで終わるかどうかをチェックして、すべてのパッケージを受け取ったかどうかを判断しています。問題は、パッケージが最後のパッケージでなくても\ r \ nで終わることがありますので、使用できません。AS3 - ソケットに返すデータがなくなったことを知る方法

次のコマンドを送信する前に完全な応答を待つ必要があるため、コマンドキューを使用しています。

除去不要なものとコード:

class CustomSocket extends Socket 
{ 

    private var _response:String; 
    private var _commandQueue:Array; 

    public function CustomSocket() 
    { 
     super(); 
     this.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 
    } 

    private function socketDataHandler(event:ProgressEvent):void 
    { 
     readResponse(); 
    } 

    private function readResponse():void { 
     var str:String = this.readUTFBytes(bytesAvailable); 
     _response += str; 
     //BUG: I cannot use this check for determining the end of packets, need to find a new one 
     if (_response.charAt(_response.length - 1) == "\n" && _response.charAt(_response.length - 2) == "\r") 
     { 
      //dispatch the result 
      commandFinished(); 
     } 
    } 

    //writes to the socket 
    private function sendRequest(request:String):void 
    { 
     _response = ""; 
     this. writeln(request); 
     flush(); 
     writeln("\r\n"); 
     flush(); 
    } 

    private function writeln(str:String):void 
    { 
     try 
     { 
      this.writeUTFBytes(str); 
     } 
     catch (e:IOError) 
     { 
      trace(e); 
     } 
    } 

    private function addCommand():void 
    { 
     //adds a command to the queue and executes it 
    } 

    private function commandFinished():void 
    { 
     //remove executed command and check if there is more commands in the queue to execute 
    } 
} 

問題が関数readResponseです。私は興味のあるものを見つけることなく、たくさんの検索を行った。

ソケットが返す合計バイト数/パケット数を知る方法はありますか?またはEOFを検出する方法、またはパッケージが最後であるかどうか

答えて

0

通常、送信しているデータの最後にヌル文字が送信されます。
これは、あなたのサーバーに探し出す決定的な特徴を与えます。

this.writeln(request + String.fromCharCode(0)); 

そして、ちょうどあなたがこの行の機能のsendRequestのドット

this. writeln(request); 

後にあなたがスペースを持っている
をお知らせするためにそして、あなたはまた、エラー処理のためにこれをしようとする場合があります。

if(this.connected){ 
    this.writeln(request + String.fromCharCode(0)); 
    this.flush(); 
}else{ 
    // do your error handling for no connection to server 
} 
関連する問題