A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Simple Java request... please

Hybrid View

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

    Simple Java request... please

    I'm in a big need of a favor. I'm well beyond my deadline but I just can't seem to get this to work. I'm looking for a java server script that's as minimalistic as possible. I'm currently using the below script, but it always seems to lock up the system at 100% cpu after a few minutes and never responds again after the first string gets sent. I need a simple java socket server that will run and respond until it receives a terminating character or I manually close it. All I'm intending to do with the script is launch the file name passed to it from a Flash AS3 socket. Thanks in advance.

    Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.io.*;
    
    public class simpleServer 
    {
    	private ServerSocket server;
    	private Socket client;
    	
    	private BufferedReader in;
    	private PrintWriter out;
    	
    	public void start()
    	{
    		create();
    		run();
    	}
    	
    	private void create()
    	{
    		try
    		{
    			server = new ServerSocket( 9001 );
    		}
    		catch ( Exception e )
    		{
    			e.printStackTrace();
    			System.exit( -1 );
    		}
    	}
    	
    	// sets up a server socket at PORT and listens to it
    	public void run()
    	{
    		try
    		{
    			client = server.accept();
    			in = new BufferedReader( new InputStreamReader( client.getInputStream() ) );
    			out = new PrintWriter( client.getOutputStream() );
    		}
    		catch ( Exception e )
    		{
    			e.printStackTrace();
    			System.exit( 0 );
    		}
    		
    		String line;
    		while ( true )
    		{
    			try
    			{
    				line = in.readLine();
    				
    				if ( line != null )
    				{
    					System.out.println( "Line received: " + line );
    				    out.write( "Line was received." );
    				    out.flush();				    
    				    Process child = Runtime.getRuntime().exec(line);
    				    line = null;
    				}
    				
    			}
    			catch ( Exception e )
    			{
    				System.out.println("Error!");
    				e.printStackTrace();
    				System.exit( 0 );
    			}
    		}
    	}
    	
    	protected void finalize()
    	{	 
    		try
    		{
    			in.close();
    			server.close();    
    		}
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    			System.exit(-1);
    		}
    	}
    	
    	/**
    	 * Entry-point.
    	 */
    	public static void main( String[] args )
    	{
    		try
    		{
    			simpleServer server = new simpleServer();
    			server.start();
    		}
    		catch ( Exception e ) {
    			// if we fail, print wtf happened and exit
    			e.printStackTrace();
    			System.exit( 0 );
    		}
    	}
    }
    Edit: I really can't afford to run one of the free servers already out there. I'm looking for something as simple as the above code that I can manipulate to accomplish what I need as small as possible.
    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
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    I have never dealt with java servers, but at first glance there is no way to determine end of data (as opposed to an error) and leave the loop.

    Musicman

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