A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: trying to use XMLSocket and a java chat server

  1. #1
    Member
    Join Date
    Mar 2001
    Posts
    42
    This is really weird... I have a java server running
    on port 8888. I use XMLSocket and connect. The onConnect
    code executes in flash. Then the server apparently sends
    data back to flash. BUT, flash just sits there! My onXML
    function never gets called!!! The server also just sits
    there saying it is waiting for flash. Although the server
    does acknowledge that the connection with flash was made.
    I don't understand why they just sit there after this.
    For some reason my XML messages can't get through.

    I tried another experiment. This time in the onConnect
    function I let flash send some XML to the server.
    The server just sits there saying it is waiting for
    data to be sent to it. Then if I close the flash app
    down in the middle of this, suddenly the info that flash
    was supposed to be sending gets through and shows up on
    my server! But this is after I've closed flash and
    broken the socket connection manually.

    I know that part of the protocol is that I'm supposed
    to send a zero-byte to tell when I'm done when using
    XMLSocket. I tried sending a null from flash when I
    was finished sending. no effect. I tried using
    out.flush() in java when I was finished sending. no effect.
    AAHHHHHH! I'm really frustrated.

    Does anyway have a clue as to what I might be doing
    wrong here???

    thanks...

  2. #2
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    Do you mind showing us your Flash Code?

    The only examples I know of this sort of stuff are in Moock's Book ActionScript a Definitive Guide.

  3. #3
    Member
    Join Date
    Mar 2001
    Posts
    42

    Smile

    I got it!

    I just downloaded Moock's flash - java chat server code.
    I wasn't aware of it's availability until now. Moock's
    code was a BIG help in finding my problem.

    Here is where the code is...
    http://www.moock.org/chat/moockComm.zip

    I'll share what I found for those interested...

    It turns out the flash actionscript was fine. The problem
    was in my java server. The tricky part about writing
    a server that will handle flash's XMLSocket seems to be
    this zero byte termination thing. It seems you have to
    handle this just right.

    Here is the useful information I found in Moock's java code.

    To send an XML string to flash you must make sure you
    do it this way...
    Code:
    PrintWriter out = new PrintWriter( socketOutStream, true);
    
    String XMLChatMessage="<SomeXML></SomeXML>"
    XMLChatMessage += '\0';  //Must send a zero byte.
    out.print( XMLChatMessage );

    To receive an XML string from flash you must make sure you
    do something like this...
    Code:
    char charBuffer[] = new char[1];
    
    //This reads one byte from the socket input stream.
    in.read(charBuffer,0,1);
    
    // --- create a string buffer to hold the incoming stream
    StringBuffer stringBuffer = new StringBuffer(8192);
    
    // --- while the stream hasn't ended
    while(charBuffer[0] != '\0')
    {
       // --- add the character to our buffer
       stringBuffer.append(charBuffer[0]);
    
       //read another byte from the socket input stream...
       in.read(charBuffer, 0 ,1);
    }
    
    //At this point stringBuffer holds the XML string
    // sent from flash.
    String XMLfromFlash = stringBuffer.toString();
    When in.read(charBuffer, 0, 1) returns a -1 the socket
    connection was broken.

    I pulled this code out of the run method in Moock's
    CSClient.java. In Moock's java chat server he makes
    a new CSClient thread for every client that connects.
    This thread just loops through and listens for incoming
    messages from flash until the socket is broken.
    (i.e. in.read(charBuffer, 0, 1) returns a -1 )
    When it gets a message it sends it out to all the other
    flash clients.

    Hope this helps anyone who may be having problems similar
    to what I was having.

    thanks to Moock...

    Thanks for your reply tupps...


  4. #4
    Senior Member catapop's Avatar
    Join Date
    Nov 2005
    Posts
    114
    woow. thank. I needed that. really helped. or you can use flash cs3 Socket class

    socket.writeUTFBytes(msg);
    socket.flush();

    in this way you will not be forced to read the stream byte after byte

    hope this helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center