Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Valentin Schmidt
Alexander Farber wrote: > After some thought... the 2nd case (not enough data arrived) > is not a problem for your code, because the handleTcpData() > will get called again - once the rest of the data arrived. > > But for the 1st case (several UTF strings arrived at once)... > Maybe I should try t

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
After some thought... the 2nd case (not enough data arrived) is not a problem for your code, because the handleTcpData() will get called again - once the rest of the data arrived. But for the 1st case (several UTF strings arrived at once)... Maybe I should try the following (and don't need a ByteA

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
On Wed, Feb 24, 2010 at 12:36 AM, Valentin Schmidt wrote: >> And if you use an "if" instead of "while", >> then you lose even more incoming data. > > how come? Ok, here your suggestion again: private function handleTcpData(event:Event):void { if(_socket.bytesAvailable) { // USE IF, NOT WHILE!

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Valentin Schmidt
> And if you use an "if" instead of "while", > then you lose even more incoming data. how come? ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
Hello Valentin, On Wed, Feb 24, 2010 at 12:06 AM, Valentin Schmidt wrote: > so then why did you start with posting code here (pasted below) that > didn't contain any exception catching? that's the crucial point: you > have to catch the exception, otherwise you would either get corrupted > UTF str

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Valentin Schmidt
some comments added to make it easier for you to spot the differences :-) private function handleTcpData(event:Event):void { if(_socket.bytesAvailable) { // USE IF, NOT WHILE! try{ // CATCH THE EXCEPTION var str:String = _socket.readUTF(); updateGUI(str); }catch(e:Error){}

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Valentin Schmidt
Alexander Farber wrote: > Yes, that's with what I had started and what doesn't work :-) so then why did you start with posting code here (pasted below) that didn't contain any exception catching? that's the crucial point: you have to catch the exception, otherwise you would either get corrupted UT

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
Yes, that's with what I had started and what doesn't work :-) On Tue, Feb 23, 2010 at 11:17 PM, Valentin Schmidt wrote: > private function handleTcpData(event:Event):void { >  if(_socket.bytesAvailable) { >    try{ >      var str:String = _socket.readUTF(); >      updateGUI(str); >    }catch(e:Er

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Valentin Schmidt
Alexander Farber wrote: > I've come up with this, but still have a flaw there: > > private function handleTcpData(event:Event):void { > var len:uint; what about this simple alternative: private function handleTcpData(event:Event):void { if(_socket.bytesAvailable) { try{ var str

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
I've added some traces there and see that this doesn't work: if (_ba.bytesAvailable >= len) { var str:String = _ba.readUTFBytes(len); updateGUI(str); // copy the remaining bytes

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
I've come up with this, but still have a flaw there: private function handleTcpData(event:Event):void { var len:uint; if (0 == _socket.bytesAvailable) return; try { _socket.readBytes(_ba, _ba.bytesAvailable, _socket

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Valentin Schmidt
> private function handleTcpData(event:Event):void { >while (_socket.bytesAvailable) { > var str:String = _socket.readUTF(); > updateGUI(str); >} > } maybe you shouldn't ignore thrown errors: AFAIK if the UTF8-data is not complete (ie.. the last UTF-8 byte sequence is truncate

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
Hello, On Tue, Feb 23, 2010 at 2:25 PM, Henrik Andersson wrote: > Doing anything but copying the data from the event to your own bytearray > buffer object is a disaster waiting to happen. > > TCP is a stream based protocol, you can get chunks of any length each time > the event is reviced. Assume

Re: [Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Henrik Andersson
Doing anything but copying the data from the event to your own bytearray buffer object is a disaster waiting to happen. TCP is a stream based protocol, you can get chunks of any length each time the event is reviced. Assume that the chunks are random length and piece them together in a buffer

[Flashcoders] bytesAvailable, readUTF() and ProgressEvent.SOCKET_DATA

2010-02-23 Thread Alexander Farber
Hello, I have multiplayer game, which reads XML data from the server: _socket = new Socket(); _socket.addEventListener(ProgressEvent.SOCKET_DATA, handleTcpData); . private function handleTcpData(event:Event):void { while (_socket.bytesAvailable) { var str:String = _socket.readUTF