Results 1 to 5 of 5
Thread: how to keep server running
- 12-21-2009, 11:38 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
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; } }
- 12-21-2009, 11:52 AM #2
Member
- Join Date
- Dec 2009
- Posts
- 36
- Rep Power
- 0
Put
out of the loop, and add an amount of sleep to the thread else your cpu is probally 100%ServerSocket svrs = new ServerSocket(8189);
- 12-21-2009, 11:56 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
when i put
ServerSocket svrs = new ServerSocket(8189);
out of the loop i get so many errores with my other code :(
- 12-21-2009, 10:52 PM #4
try to declare "public static ServerSocker svrs" field in your EchoServer class.
and call the constructor before the main loop once.
- 12-22-2009, 10:11 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
War file of my web application is not running through TOMCAT SERVER
By jason.3dmagic in forum EclipseReplies: 3Last Post: 12-24-2009, 07:04 AM -
how to run web server w/out running at eclipse?
By anthrax in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-13-2009, 09:06 AM -
rmi when running server and client
By anukatoch in forum NetworkingReplies: 1Last Post: 01-28-2009, 12:03 PM -
Maintain same session in two application running on same server
By dsiwal in forum Threads and SynchronizationReplies: 2Last Post: 06-10-2008, 06:38 AM -
running multiple server
By amitnayak1 in forum Advanced JavaReplies: 3Last Post: 06-05-2008, 04:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks