5 Replies Latest reply on May 24, 2009 10:41 AM by Gregg_B

    XMLSocket problem

    Gregg_B

      Hi All,

      I'm trying to use the XMLSocket object to send some simple strings to the Java Server.

      (The java server is just a simple socketServer included in the Flex 3.0 API documentation for XMLSocket)

      Each time I send the string to the server it freezes it.

      For example, I have this in my .as file.

       

      private function sendToXMLSocket(xmlMsg:String):void
      {
              import flash.net.XMLSocket;
              import flash.net.Socket;
                 
              var sock:XMLSocket = new XMLSocket("localhost", 7778);
              sock.send(xmlMsg);   
      }

      I have just a form with one button on it and the above method is executed when I click on that button.

      The xmlMsg string is just this for now: "greggreggreg\n"

      The JavaSimpleServer receives it and I can see it printed to the console but when I click one more time on the button nothing gets accross.

      The messages (and number of messages depends on how many times I click on the button) don't get transmitted unless

      I click on the X to close the window and terminate the program.

      Only then I can see all of the messages on the other side.

      I can't figure what seems to be a problem. I'm thinking that this is happening (maybe) due to the fact that the XMLSocket appends a zero byte at the end of each message sent to the server. Is this a problem?

      By the way, original code for the JavaSimpleServer  was closing an incoming socket connection right after the first message got to it. I removed that line of code (incoming.close()) becasue I'd like to be able to send as many messages as I can. So I need to keep the socket connection open.

      (This all works for me when I send messages from Java client.)

      Can someone help?

      Thanks

      Greg

        • 1. Re: XMLSocket problem
          Gregg_B Level 1

          So I switched from XMLSocket to just plain Socket.

          I have this now:

            var sock:Socket = new Socket();
                      sock.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
                      sock.connect("localhost", 7778);
                      sock.writeUTFBytes(xmlMsg);
                      sock.flush();

           

          But nothing gets transmitted unless I close the Flex app.

          I don't get this. What seems to be a problem?

          I read an API docs twice and it says that writeUTFBytes (or any other write methods) are just putting stuff in a buffer

          the flush() is the one that signals that the buffer content needs to be sent.

          I'm doing just that and still I get nothing on the other side unless I close the Flex app.

          Any suggestion?

          Thanks

          Greg

          • 2. Re: XMLSocket problem
            _Natasha_ Level 4

            Hi Greg,

             

            You can write to socket only after Event.CONNECT event dispatched. As I understand for XMLSocket you haven't call connect() method at all.

            See example from docs.

            http://livedocs.adobe.com/flex/3/langref/flash/net/XMLSocket.html#includeExamplesSummary

            • 3. Re: XMLSocket problem
              Gregg_B Level 1

              No Natasha,

              The line:  var sock:XMLSocket = new XMLSocket("localhost", 7778);

              already establishes the connection. Instead of calling the connect() method I just use the constructor of XMLSocket to this job.

              It works and I can send stuff to the server. I can also see it on the server. What causes the problem is the additional

              ZERO Byte attached to the XML string sent. I don't know whose idea was to create such object (the XMLSocket object)

              that gets this additional zero byte at the end. I think is stupid and I'd fired the programmer that design this..........

              I got around the problem using the Socket object instead.

              Thanks

              • 4. Re: XMLSocket problem
                Lopo Nopo Level 1

                Hahaha!

                 

                By the way - why do you create a new connection per message every time?

                I'd fire a programmer who does that in a commercial product as well

                • 5. Re: XMLSocket problem
                  Gregg_B Level 1

                  Simply because I can.

                  It doesn't matter at the moment.

                  This suppose to be only a test/sample program for someone smart here to tell me how to married

                  flash client with java server. (how to get the XML string to the other side)

                  I got it to work, however, the problem remains for those trying to do the same.

                  Did you write it?

                  LOL