2 Replies Latest reply on Aug 11, 2008 10:47 PM by tiaan.wessels

    Concurrent Socket Traffic

    tiaan.wessels Level 1
      I have an application connected to an IPCBUS over which it receives events at unpredictable intervals. My app also has various GUI elements allowing the user to interact with a backend RPC server. Every now-and-then I find the response to an RPC messed up and from tracing it would seem to coincide with incoming traffic from the bus. The two pieces of data is non-related and carried over different socket connections. My question is whether flex is supposed to handle this gracefully or is it documented somewhere that you are allowed to have traffic on a single socket at any one time. Of course the two aspects uses different callback functions to handle their data and everything is heavily based on extending the EventDispatcher class.
        • 1. Re: Concurrent Socket Traffic
          Dr. Fred Mbogo Level 1
          I haven't yet tried doing network programming in Flash, but as a longtime Winsock/sockets programmer, this smells to me like a bug in your code. It's common for new network programmers to misunderstand how TCP/IP works, thus causing problems when they try to use it.

          The most important thing to understand is that TCP is a stream protocol. There are no packets in TCP, at the application level. If the remote host makes three "send" calls, one with 50 bytes, the next with 100, and the next with 150, your program can receive all 300 bytes in a single receive call, or one byte at a time in 300 separate receives, or anything in between. How those bytes are packetized over the wire is, effectively, totally outside your application's control, due to optimizations in TCP like the Nagle and delayed-ACK algorithms. If there is anything smarter than a switch between the two hosts, things can get even crazier.

          So, if you're expecting a "packet" from the server, you need to somehow be able to know when you've received the whole thing. The two common ways of doing that are length-prefixing and delimiting.

          An example of length-prefixing is to always send 2 bytes at the start of every packet with the length of that packet, as a binary number. Beware when doing this that different machine types have different byte orders for integers at the binary level. You need to agree on a standard. Or, you can limit yourself to 256-byte packets by sending just a single byte length prefix, and avoid the mess.

          An example of delimiting is to put some special byte or group of bytes between packets that never occurs in regular data. A lot of the Internet protocols do this, with linefeed characters.

          And then there are the protocols like HTTP that do both: the header lines are delimited with linefeeds, and then you have an optional block of content following the headers, whose size is declared in the Content-Length header in a form of length prefixing.

          If you know all this, and you're sure you've done it right, post some code so we can offer more than general advice, as I've done.
          • 2. Re: Concurrent Socket Traffic
            tiaan.wessels Level 1
            Thanks for the long reply. I am fortunately not new to network programming (you'll even find a patch to the linux kernel x25 stack under my name if you dig hard enough - sorry, seems as though I need to establish credentials).

            I inherited a large flex application when a person left our company and am currently trying to fix some intermittent networking related problems in the program. After a couple of days debugging now it is starting to seem to me as if the flash runtime mixes up network traffic when dispatching events but from experience I have learnt not to question the compiler or tools but rather your own program. Therefore the first thing and really the crux of my question is: is there no documented limitation somewhere in the 3000+ pages of flex/actionscript documentation stating that you are only allowed communication on a single socket at any one time. If there is no such limitation and no documented bug in the flash runtime then I know I should dig deeper.

            It is unfortunately impossible for me to upload example code as this is thousands of lines of code to illustrate the problem so I think all I need answered is the question regarding any documented limitations on concurrent socket communications if there is a flex network programming expert out there perhaps reading this.