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

    Default how to keep server running

    hi this is my server code, I want that the server keeps running even if there is not client to connect to the server I know i have to put a loop to do this but i tried alot but didnt find to which part to put the loop any help please?

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class EchoServer
    {
            
        public static void main(String[] args)
    
       {
         while(true){
          try
          {
             // server socket is not a Socket
             ServerSocket svrs = new ServerSocket(8189);
             // wait for client connection
             Socket s = svrs.accept();
             try
             {
                
                InputStream inStream = s.getInputStream();
                OutputStream outStream = s.getOutputStream();
    
                /**
                 * Once the client is connected to the server, to make sure that the connection is well established
                 * the Server sents a message of confirmation to the client"Hello you are connected to the Server"
                 */
    
                Scanner in = new Scanner(inStream);
                PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
    
                String serverMove=getServerMove();
                out.println(serverMove);
    
               //out.println("Hello! You are connected to the Server");
    
               String name = in.nextLine();
               System.out.println(name);
    
               String ClientMove = in.nextLine();
               System.out.println(ClientMove);
    
              String win=determineRoundWinner(ClientMove, serverMove);
    
              out.println(win);
                
             }
    
    
             finally
             {
                s.close();
             }
          }
    
          catch (IOException e)
          {
             e.printStackTrace();
          }
        break; }
        }
        static String getServerMove() {
    	double compMoveInt;
    	String compMove;
    
    	// Generate a random move for the
    	// computer.
    	compMoveInt = Math.random();
    
    	// Convert the random integer into a
    	// string that represents the computer's
    	// move.
    	if(compMoveInt >= 0.0 && compMoveInt <= 0.33) {
    	    compMove = "stone";
    	}
    	else if (compMoveInt >= 0.3 && compMoveInt <= 0.6) {
    	    compMove = "paper";
    	}
    	else {
    	    compMove = "scissors";
    	}
    
    	return compMove;
        } 
            /**
         * Compare the user's move to the computer's
         * move to determine the winner of this round.
         *
         * @param userMove the user's move.
         * @param compMove the computer's move.
         * @return  1 if the computer wins.
         *          0 if it is a tie.
         *         -1 if the user wins.
         */
        static String determineRoundWinner(String ClientMove,
    				    String compMove) {
    	String win = null;
    	// Check for ties.
    	if (compMove.equals(ClientMove)) {
    	   win="Draw";
    	}
    
    	// if it is not a tie check for all the ways the
    	// computer can win.
    	// Rock smashes scissors...
    	else if (compMove.equals("stone") &&
    		 ClientMove.equals("scissors")) {
    	    win="Compuer Wins Better Luck Next Time :)";
                
    	}
    	// Paper covers rock...
    	else if (compMove.equals("paper") &&
    		 ClientMove.equals("stone")) {
    	    win="Compuer Wins Better Luck Next Time :)";
    	}
    	// Scissors cut paper...
    	else if (compMove.equals("scissors") &&
    		 ClientMove.equals("paper")) {
    	    win="Compuer Wins Better Luck Next Time :)";
    	}
    	// Its not a tie and the computer did not
    	// win so the user must have won!
    	else {
    	    win="You Win ";
    	}
    
    	return win;
        }
        
          
    }

  2. #2
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    Put
    ServerSocket svrs = new ServerSocket(8189);
    out of the loop, and add an amount of sleep to the thread else your cpu is probally 100%

  3. #3
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default

    when i put
    ServerSocket svrs = new ServerSocket(8189);
    out of the loop i get so many errores with my other code :(

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    try to declare "public static ServerSocker svrs" field in your EchoServer class.
    and call the constructor before the main loop once.

  5. #5
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    offcourse you don't have to put the whole line outside,
    declare: ServerSocket sc;
    and before the while loop u put sc = new ServerSocket(#);

Similar Threads

  1. Replies: 3
    Last Post: 12-24-2009, 08:04 AM
  2. how to run web server w/out running at eclipse?
    By anthrax in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-13-2009, 10:06 AM
  3. rmi when running server and client
    By anukatoch in forum Networking
    Replies: 1
    Last Post: 01-28-2009, 01:03 PM
  4. Maintain same session in two application running on same server
    By dsiwal in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-10-2008, 07:38 AM
  5. running multiple server
    By amitnayak1 in forum Advanced Java
    Replies: 3
    Last Post: 06-05-2008, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •