A Flash Developer Resource Site

Results 1 to 9 of 9

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

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

    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

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Alright so I've got it to work... sort of. It's still slow as ever, and I'd really like to speed it up. What I didn't know is that if the execution string isn't an exe you have to specify one. So instead of doing
    Code:
    Runtime.getRuntime().exec("C:\\vid.avi");
    You have to qualify an app name:
    Code:
    Runtime.getRuntime().exec("C:\\Program Files\\Windows Media Player\\wmplayer.exe C:\\vid.avi");
    The problem I'm running into is that some video titles aren't just solid letters. They might look like "50 first dates.avi" or "50.first.dates.avi". Naturally, Java pukes all over this when I try to execute the files. Is there a way to escape these characters in the string so that I can use whatever the user specifies for a filename?
    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

  3. #3
    Senior Member
    Join Date
    May 2009
    Posts
    138

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Been there. Doesn't work for me. What I can't find is anywhere that talks about doing this with AS3 AND AIR. There is a difference.
    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

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    got a spare $185?

    http://www.shu-player.com/buy
    lather yourself up with soap - soap arcade

  6. #6
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Yeah I saw that... not worth the price tag. I'm close to getting this to work, I just don't understand why it takes so long for me to connect to a socket on local host.
    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

  7. #7
    Senior Member
    Join Date
    May 2009
    Posts
    138
    Okay, obviously I didn't try it myself This is likely to be a stupid question, but have you just tried using double slashes? "50\\ First\\ Dates.avi"

  8. #8
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    No but I just did. Doing it that way only opens windows media player.

    Code:
    //...
    				try {
    					String str = "50 first dates.avi";
    					String out = "C:\\Program Files\\Windows Media Player\\wmplayer.exe "+ str.replaceAll(" ", "\\\\ ");
    					System.out.println("Executing: " + out);
    					Runtime.getRuntime().exec(out);
    				} catch (Exception err) {
    					System.out.println("Error. " + err);
    				}
    //...
    //Trace: Executing: C:\Program Files\Windows Media Player\wmplayer.exe 50\ first\ dates.avi
    //
    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

  9. #9
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    One final question and I'm hoping you guys can answer it because it's likely a small error. As I've said, the actual time it takes the socket to respond to the data sent is 30-60 seconds. I noticed, however, that as soon as the socket.send() goes through from flash, if I close the app, I see instant results in the java output window. Is there something I'm forgetting to do in Flash? Something like a flush command or something?
    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