dcsimg
A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: communication with Java (non-blocking socket)

Threaded View

  1. #1
    Junior Member
    Join Date
    Mar 2008
    Posts
    26

    communication with Java (non-blocking socket)

    I have a flash animation that communication with Java process through a socket.

    I have a while loop that keeps collecting live sensor data from Java from the socket, then draw the sensor data on flash.

    But when I run the flash, it just crash / freeze, how do I use multiple thread in flash so one thread is collect data from sensor in an infinite loop, other thread will draw the animation on screen once data is collected?

    Code:
    mySocket = new XMLSocket();
    
    mySocket.onConnect = function(success)
    {
    	if (success){
    		trace('connected');
    		sendMsg();
    	}
    	else
    		trace('failure');
    }
    
    mySocket.onClose = function()
    {
    	trace('close');
    }
    
    XMLSocket.prototype.onData = function(msg)
    {
    	trace('data: ' + msg);
    }
    
    mySocket.connect("localhost", 9999)
    
    function sendMsg()
    {
    	    var i:Number = 0;
    		trace('sendMsg');
    		while(true){
    			i++;
    			mySocket.send('msg-'+ i +'\n');
    			sleep(1);
    		}
    }
    Code:
    import java.io.*;
    import java.net.*;
    
    public class SimpleServer 
    {
            
        public static void main(String args[])
        {
            // Message terminator
            char EOF = (char)0x00;
            
            try
            {
                // create a serverSocket connection on port 9999
                ServerSocket s = new ServerSocket(9999);
                
                System.out.println("Server started. Waiting for connections ...");
                // wait for incoming connections
                Socket incoming = s.accept();
                
                BufferedReader data_in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                PrintWriter data_out = new PrintWriter(incoming.getOutputStream());
                
                data_out.println("Welcome! type EXIT to quit." + EOF);
                data_out.flush();
                
                boolean quit = false;
                
                // Waits for the EXIT command
                while (!quit)
                {
                    String msg = data_in.readLine();
                    
                    if (msg == null) quit = true;
                    
                    if (!msg.trim().equals("EXIT"))
                    {
                    	System.out.println(msg);
                        data_out.println("You sayed+++: <b>" + msg.trim() + "</b>" + EOF);
                        data_out.flush();
                    }
                    else
                    {
                        quit = true;
                    }
                }
            }
            catch (Exception e)
            {
                System.out.println("Connection lost");
            }
        }
        
    }
    Last edited by tobycraftse; 05-20-2008 at 06:01 PM.

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