A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: AS3/AIR 1.5 -SIMPLE- Socket Server with Java

Threaded View

  1. #1
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377

    AS3/AIR 1.5 -SIMPLE- Socket Server with Java

    I was just wondering if there was any reason this wasn't working. Basically I'm writing an application (let's call it a game so I can keep this thread here because it probably would be useful for games too) and I originally wanted to use FSCommand to launch video files. Unfortunately, until AIR 2.0 this is impossible and you need an external process running so, knowing java can launch files, I decided to write a simple socket server to pass the file path to and have Java execute it for me. Here's the AppSocket class:

    Code:
    package com.hsg.util {
    	import flash.display.Sprite;
        import flash.events.*;
        import flash.net.XMLSocket;
    
        public class AppSocket extends Sprite {
            private var hostName:String = "localhost";
            private var port:uint = 8080;
            private var socket:XMLSocket;
    		private var sendData:String;
    
            public function AppSocket(data:String) {
    			sendData = data;
                socket = new XMLSocket();
                configureListeners(socket);
                socket.connect(hostName, port);
            }
    
            public function send():void {
                socket.send(sendData);
            }
    
            private function configureListeners(dispatcher:IEventDispatcher):void {
                dispatcher.addEventListener(Event.CLOSE, closeHandler);
                dispatcher.addEventListener(Event.CONNECT, connectHandler);
                dispatcher.addEventListener(DataEvent.DATA, dataHandler);
                dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            }
    
            private function closeHandler(event:Event):void {
                trace("closeHandler: " + event);
            }
    
            private function connectHandler(event:Event):void {
                trace("connectHandler: " + event);
            }
    
            private function dataHandler(event:DataEvent):void {
                trace("dataHandler: " + event);
            }
    
            private function ioErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event);
            }
    
            private function progressHandler(event:ProgressEvent):void {
                trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
            }
    
            private function securityErrorHandler(event:SecurityErrorEvent):void {
                trace("securityErrorHandler: " + event);
            }
        }
    }
    And here is my single file Java socket server:

    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(8080);
    			
    			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();
    				try {
    					System.out.println("Executing: " + msg);
    					Process p = Runtime.getRuntime().exec(msg);
    				} catch (Exception err) {
    					System.out.println("Error.");
    				}
    				
    				if (msg == null) quit = true;
    				
    				if (!msg.trim().equals("EXIT"))
    				{
    					data_out.println("You sayed: <b>"+msg.trim()+"</b>"+EOF);
    					data_out.flush();
    				}
    				else
    				{
    					quit = true;
    				}
    			}
    		}
    		catch (Exception e)
    		{
    			System.out.println("Connection lost");
    		}
    	}
    }
    And here's how I call it:

    Code:
    		private function launchFile(path:String) {
    			var sock:AppSocket = new AppSocket(path);
    			sock.addEventListener(Event.CONNECT, blah);
    		}
    		
    		private function blah(de:DataEvent) {
    			trace("TEST DATA BEING SENT");
    			de.target.send("TEST DATA");
    		}
    Now, it will show the path in java, but my problem is that it takes something like 10-30 seconds for Java to finally display the message it received. Is there a way to speed this up? Also, it's failing to execute the file. Flash does receive the message "Welcome! Type EXIT to quit.". It will trace out the path I give (i.e C:\Users\Public\Videos\vid.avi) but it never executes. After it displays the path it immediately terminates the java program. Output:

    Code:
    Server started. Waiting for connections...
    Executing: C:\Users\Public\Videos\vid.aviError. java.lang.NullPointerException
    Connection lost
    
    Process completed.
    If anyone could figure what I'm doing wrong here, I'd much appreciate it. This app is so far behind schedule it's not even funny because I didn't realize they changed FSCommand capability in AS3/AIR.

    I am working with Adobe CS4 Professional, AIR 1.5, AS3, on Windows 7.
    Last edited by ImprisonedPride; 10-15-2009 at 02:14 PM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

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