multi-client server [problem with code - please have a look]

Reply

Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

multi-client server [problem with code - please have a look]

 
0
  #1
Mar 13th, 2008
Hello,

I urgently need help, im trying to complete a class which will be able of multiple - clients but is not doing the required puprose. Can anyone be assistance of what is needed to add for handling the clients ??

Thanks in advance!!
  1. import java.util.ArrayList;
  2. import java.util.concurrent.Semaphore;
  3.  
  4. class LODServer
  5. {
  6. ServerBase<ClientHandler> server = null;
  7.  
  8. /*
  9.   ** Once the given number of players has connected,
  10.   ** the instance of ServerBase<ClientHandler> returns
  11.   ** an array of them.
  12.   */
  13. ArrayList<ClientHandler> clients = null;
  14.  
  15. /*
  16.   ** This is the data that encodes the state of the game.
  17.   ** The labyrinth is stored in a 2D array with
  18.   ** numbers corresponding to the contents of each location.
  19.   */
  20. static final int TREASURE = 1;
  21. /* Larger positive numbers are several lots of treasure */
  22. static final int EMPTY = 0;
  23. static final int HEALTH = -1;
  24. static final int LANTERN = -2;
  25. static final int SWORD = -3;
  26. static final int ARMOUR = -4;
  27. static final int EXIT = -5;
  28. static final int WALL = -6;
  29.  
  30. int mapWidth = 5;
  31. int mapHeight = 5;
  32. int map[/*mapHeight*/][/*mapWidth*/] =
  33. {
  34. {WALL, WALL, ARMOUR, WALL, WALL },
  35. {WALL, EXIT, EMPTY, TREASURE, TREASURE},
  36. {LANTERN, TREASURE, EMPTY, HEALTH, LANTERN },
  37. {WALL, SWORD, EMPTY, ARMOUR, TREASURE},
  38. {WALL, WALL, SWORD, WALL, WALL }
  39. };
  40.  
  41. boolean gameStarted = false;
  42. boolean gameFinished = false;
  43. int goal = 2; // The amount of treasure to collect
  44.  
  45. /*
  46.   ** The server has to be able to wait until the end of the player's turn.
  47.   ** It uses a semaphore to do this.
  48.   ** The player acquires the semaphore at the start of the turn
  49.   ** and releases it at the end of the turn.
  50.   ** This allows the server to wait until the end of the player's turn by
  51.   ** simply waiting for the semaphore to be released.
  52.   */
  53. Semaphore playerTurn = new Semaphore(1);
  54.  
  55.  
  56. public void setup (int port, int players, String map) {
  57. // Limit to one player
  58. if (players > 1) {
  59. System.err.println("The current implementation is limited to one player.");
  60. players = 1;
  61. }
  62.  
  63. // Create the object to handle the incoming connections
  64. // ( you don't have to understand how this works )
  65. // If the last arguement is true then all incoming and outgoing messages
  66. // to the instances of ClientHandler will be logged.
  67. // Set to false if you don't want this.
  68. server = new ServerBase<ClientHandler>(ClientHandler.class, players, port, true);
  69.  
  70.  
  71.  
  72. // Any set up for the game that can be done
  73. // before the clients connect goes here...
  74.  
  75. // Map name is currently ignored
  76. System.err.println("Map loading is not implemented, \"" + map + "\" ignored.");
  77.  
  78.  
  79.  
  80. // Start accepting incoming connections.
  81. System.out.print("Waiting for players to connect ... ");
  82. server.start();
  83. }
  84.  
  85. public void play () throws InterruptedException {
  86.  
  87. // Wait for all of the clients to connect
  88. clients = server.getConnections();
  89. System.out.println(" done.");
  90.  
  91. // Any set up for the game that is specific
  92. // to the clients goes here.
  93.  
  94.  
  95. // Give each ClientHandler a reference to this object
  96. // and thus access to the game data
  97. for (ClientHandler c : clients) {
  98. c.server = this;
  99. }
  100.  
  101. // Fix the player's starting location
  102. clients.get(0).x = 2;
  103. clients.get(0).y = 2;
  104.  
  105. // Let each client know the amount of treasure it needs
  106. for (ClientHandler c : clients) {
  107. c.serverGoal(goal);
  108. }
  109.  
  110. // Play the game!
  111. System.out.println("Starting the game.");
  112. gameStarted = true;
  113. while (gameFinished == false) {
  114.  
  115. // Check that there is at least one player connected
  116. if (clients.size() <= 0) {
  117. System.out.println("Game abandoned");
  118. break;
  119. }
  120.  
  121. // Each player takes a turn
  122. //for (ClientHandler c : clients) {
  123. ClientHandler c = clients.get(0);
  124.  
  125. // Start turn
  126. System.out.println("Starting the turn of player \"" + c.name + "\"");
  127. c.startTurn(); // During this, c will acquire() the playerTurn semaphore
  128.  
  129. // Wait for the player to finish their turn
  130. // (they will release() the semaphore at the end of their turn)
  131. try {
  132. playerTurn.acquire();
  133. } catch (InterruptedException e) {
  134. // This shouldn't happen in normal operation
  135. // If it happens, it is because this code
  136. // has been used in a larger bit of software
  137. // thus the wrapper can deal with this condition
  138. throw(e);
  139. }
  140. System.out.println("End of turn");
  141.  
  142. // Check to see if the current player has won
  143. // (gameFinished is set to true in ClientHandler.clientEndTurn())
  144. if (gameFinished == true) {
  145.  
  146. System.out.println("Game over, \"" + c.name + "\" is the winner");
  147.  
  148. // Tell the player they have won
  149. c.serverMessage("Congratulations " + c.name + " you won!");
  150. c.serverWin();
  151.  
  152. // Tell everyone else that they haven't
  153. for (ClientHandler d : clients) {
  154.  
  155. if (d != c) {
  156. d.serverMessage("Sorry " + d.name + " it looks like " + c.name + " beat you.");
  157. d.serverLose();
  158. }
  159.  
  160. }
  161.  
  162. }
  163.  
  164. // Release the semaphore so that the next player can start their turn
  165. playerTurn.release();
  166. //}
  167.  
  168.  
  169. }
  170.  
  171. return;
  172.  
  173. }
  174.  
  175. // A simple helper function
  176. public static void printUsageAndDie(String error) {
  177. System.err.println(error);
  178. System.err.println("Usage:");
  179. System.err.println("\tjava LODServer <port> <players> <map>");
  180. System.exit(1);
  181. }
  182.  
  183. // The main method just handles the command line arguements
  184. // creates one instance of LODServer and starts it running
  185. public static void main(String args[])
  186. {
  187.  
  188. // Check the command line arguements
  189. if (args.length != 3) {
  190. printUsageAndDie("Incorrect number of command line arguements");
  191. } else {
  192. // Parse the command line arguements
  193. int port = Integer.parseInt(args[0]);
  194.  
  195. // Only ports 1025 to 65535 are suitable
  196. if ((port <= 1024) || (port >= 65536)) {
  197. printUsageAndDie("Invalid port number");
  198. }
  199.  
  200. int players = Integer.parseInt(args[1]);
  201. if (players < 0) {
  202. printUsageAndDie("Invalid number of users");
  203. }
  204.  
  205. // Create a LODServer object to run the game
  206. LODServer s = new LODServer();
  207.  
  208. // Set it up
  209. s.setup(port,players,args[2]);
  210.  
  211. // Go!
  212. try {
  213. s.play();
  214. } catch (InterruptedException e) {
  215. // Something has gone wrong - abort
  216. System.err.println("Caught InterruptedException(?)");
  217. System.exit(1);
  218. }
  219. }
  220. }
  221. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: mzaiady is an unknown quantity at this point 
Solved Threads: 0
mzaiady mzaiady is offline Offline
Newbie Poster

Re: multi-client server [problem with code - please have a look]

 
0
  #2
Mar 14th, 2008
What is the proplem exact you are facing, and also i noticed you are commenting the part in the code that loops over the clients to give each players his turn

// Each player takes a turn
//for (ClientHandler c : clients) {
ClientHandler c = clients.get(0);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC