Results 1 to 5 of 5
  1. #1
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default Making a program auto restarter in java!

    As practice to improve my java skills i have decided to make a game server auto restarter for quake that checks every now and then if the server is online by reading port up to now it seems to work though it overloads the cpu since i got no timer and it goes like loop every second

    i'd like to ask if theres any other way except using java timer because its too complicated to make the java run the loop every 30 seconds..


    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.util.Scanner;
    
    class srvRestarter
    {
    
    	//main method
    	public static void main(String[] args) 
    	{ 
    	
        //declaring the variables
     	
        int port;
        int t;
    
    	String cmd;
    	
    	//initializing
    	t= 1;
    	
        
    	//scanner
    	Scanner Input = new Scanner(System.in); 
    	 
        
    	//insert server's port
    	System.out.println("Please type your server's port");
    	
        //scan the text
    
        port= Input.nextInt();
    	
    
        
        //executing the server
        
    
        
         try
         {
         
          Runtime rt = Runtime.getRuntime() ;
    
          Process p = rt.exec("jk2ded.exe +net_port" + port + "+exec server.cfg") ;
          }
          catch(IOException e)
          {
    	      
    	      
          }
        
        
        do
    	{
    	   t++;
    	   
    	   
    	   
    
    	   try 
           {  
         	
    	       
    	    //serversocket constructor
    	    ServerSocket status = new ServerSocket(port);
       
            status.close();  
            status= null;  
           
            try
            {
         
            Runtime rt = Runtime.getRuntime() ;
    
            Process p = rt.exec("quake or anyother quake based game .exe +net_port" + port + "+exec server.cfg") ;
            }
            catch(IOException e)
            {
    	      
    	      
            }
        
            
            } 
            catch (IOException e) 
            {  
            
    	        //do nothing continue the loop
    	          
            }  
    	      
    	   
    	   
    	   
    			
        }
    	while(t > 0);
    	
    	
    	
      }
    }
    i thought of putting For loop before the do while but with that i guess you can never have specific timing since it depends on the CPU power of each pc to execute the for loop

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    You could have your program sleep from 30 sec, then check, repeat...

    Java Code:
    catch (IOException e) 
            {  
            
    	        //do nothing continue the loop
    	          
            }  
    	      
    	   
    	Thread.sleep(30000);   
    	   
    			
        }
    	while(t > 0);

  3. #3
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    it doesn't work it gives compile error, possibly i should add to the code some flow control? try{ thread.sleep(xxx) } catch( some kind of exception?) { }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by g123456 View Post
    it doesn't work it gives compile error, possibly i should add to the code some flow control? try{ thread.sleep(xxx) } catch( some kind of exception?) { }
    Yes you should.

    P.S Timer is not complicated at all. Just go through a tutorial for it.

    Here's one:
    Job Scheduling in Java - O'Reilly Media

  5. #5
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    ok thank you i will look later at it though it seems like this thread.sleep works for the moment

Similar Threads

  1. Making a program out of a Java Applet
    By Bomber_Will in forum Java Applets
    Replies: 1
    Last Post: 07-16-2009, 03:54 AM
  2. Making the program
    By pheonix in forum Advanced Java
    Replies: 4
    Last Post: 04-18-2009, 07:36 AM
  3. Need help with Java classes for making a program.
    By TheDarkReverend in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 05:50 AM
  4. [SOLVED] Making JAVA Program excutable
    By kirly in forum Advanced Java
    Replies: 10
    Last Post: 10-07-2008, 04:57 AM
  5. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 09:54 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
  •