0

I am trying to push a series of strings in an array collection to the client application via socket. But, sometimes, the strings does not get pushed out one by one. For example, the client application would receive the data as so: array[0], array[1], array[3], array[5]....

As you can see, some of the strings were skipped. Below is my code:

var i:int;

for (i = 0; i < ac.length; i++){
  socket.writeUTF(ac.getItemAt(i).toString());
  socket.flush();
}

When I trace the bytes available, this is the result:

[string 1]
bytes.available = 851

[string 3]
bytes.available = 1406
.
.
.

According to some, it is due to the flush() never gets called quick enough for subsequent data. I am not sure about this. Please help.

Victor Yew
  • 35
  • 9
  • What happens if you call flush() outside of the loop, instead of on every iteration of the loop? – Sunil D. Aug 30 '13 at 13:46
  • In order to send the string one by one, the flush() needs to be called right after every writeUTF – Victor Yew Aug 31 '13 at 00:43
  • If the theory above is true (calling `flush()` so quickly is the problem) then shouldn't you modify your logic to avoid that? I think my suggestion above or inserting a delay would then solve it. Besides, it's more efficient to call `flush()` once rather than n-times. If I were you, I would at least confirm that this is the issue. – Sunil D. Aug 31 '13 at 02:04
  • Finally, you might look into using the [outputProgress](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html#event:outputProgress) event. Theoretically you should get one or more of these events after each call to `flush()`. So you'd wait for the event, check to see if there are still bytes waiting to be sent to the network layer, and don't write/flush the socket until the previous `flush()` completed. But again calling `flush()` a little as possible would probably be better. – Sunil D. Aug 31 '13 at 02:05
  • I've tried using a timer to set the delay, and yes, the result is close to what I wanted. But, I am developing a real-time application where delay is not encouraged. Plus, when the arraycollection is big enough, the problem tends to occur again. – Victor Yew Sep 02 '13 at 00:43
  • Using the solution with the outputProgress event that I alluded to is your best bet, since you will be able to start the next write/flush sequence as soon as the last one finishes. – Sunil D. Sep 02 '13 at 04:57

1 Answers1

0

Send your strings together with their index, so you can join them in the remote process in their original order.

Something like that:

for (i = 0; i < ac.length; i++){
   socket.writeUTF("[" + i + "]:" + ac.getItemAt(i).toString());
   socket.flush();
}
splash
  • 13,037
  • 1
  • 44
  • 67
  • The order of the string is never the problem. The problem is some of string have gone missing all together like the result i shown above – Victor Yew Aug 31 '13 at 00:46