Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2008, 12:51 PM   #21
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

the client gets connected and the data gets sent from the Server to the Client just fine but the trouble comes when trying to send data from the client to the server. I have the sendData method inside client but when i enter a string to send to the Server i think i need a receive method in the Server class to get the data correct? When i call the receive method inside Server it hangs like it is waiting to receive something from the client. But I am pretty sure the connections are right or else the Server would not send the Client something right? Here is the code
the Server class
java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. //import java.net.*;
  5.  
  6. public class Server
  7. {
  8. private static Socket connection;
  9. private static ObjectOutputStream output;
  10. private static ObjectInputStream input;
  11.  
  12. public static void main(String[] args) throws java.io.IOException
  13. {
  14. ServerSocket s = new ServerSocket(1234);
  15. String string = "";
  16. while(true)
  17. {
  18. connection = s.accept();
  19. System.out.println("client connected");
  20. streams();
  21. send("Send this to client");
  22. //System.out.println("Listenning...");
  23. //(new ServerThread(s.accept())).start();
  24. //System.out.println("Spawned Thread.");
  25. } // end while
  26. } // end main
  27.  
  28. // establish the streams
  29. private static void streams() throws IOException
  30. {
  31. output=new ObjectOutputStream(connection.getOutputStream());
  32. output.flush();
  33. input=new ObjectInputStream(connection.getInputStream());
  34. } // end streams
  35.  
  36. private static void send(String or_variable)
  37. {
  38. try
  39. {
  40. output.writeObject("String" + or_variable);
  41. output.flush();
  42. } // end try
  43. catch(IOException ioException)
  44. {
  45. }
  46. } // end send
  47.  
  48. private static String receive() throws IOException
  49. {
  50. String test = "";
  51. try
  52. {
  53. test = (String)input.readObject();
  54. System.out.print("\n" + test);
  55. } // end try
  56. catch(ClassNotFoundException classNotFoundException)
  57. {
  58. System.out.print("\nUnknown object recieved");
  59. } // end catch
  60. return test;
  61. } // end receive
  62. } // end Server
Client class
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class Client
  6. {
  7.  
  8. private Socket client;
  9. private ObjectOutputStream output;
  10. private ObjectInputStream input;
  11.  
  12. //Client constructor
  13. private void client()
  14. {
  15. try
  16. {
  17. connect();
  18. streams();
  19. processConnection();
  20. } // end try
  21. catch (EOFException eofException)
  22. {
  23. System.err.println("Client terminated connection");
  24. } // end catch
  25. catch (IOException ioException)
  26. {
  27. ioException.printStackTrace();
  28. } // end catch
  29. finally
  30. {
  31. closeConnection();
  32. } // end finally
  33. } // end client
  34.  
  35. public void connect() throws IOException
  36. {
  37. System.out.print("Attempting Connection\n");
  38. client = new Socket(InetAddress.getByName("192.168.1.102"), 1234);
  39. System.out.print("Connected to: " + client.getInetAddress().getHostName());
  40. } // end connect
  41.  
  42. public void streams() throws IOException
  43. {
  44. output=new ObjectOutputStream(client.getOutputStream());
  45. output.flush();
  46. input=new ObjectInputStream(client.getInputStream());
  47. System.out.print("\nStreams Estabolished\n");
  48. } // end streams
  49.  
  50. public void processConnection() throws IOException
  51. {
  52. String test = "";
  53. do
  54. {
  55. try
  56. {
  57. test = (String)input.readObject();
  58. System.out.print("\n" + test);
  59. } // end try
  60. catch(ClassNotFoundException classNotFoundException)
  61. {
  62. System.out.print("\nUnknown object recieved");
  63. } // end catch
  64. }while(!test.equals("Server Connection Lost")); // end do while
  65. } // end processConnection
  66.  
  67. public void closeConnection()
  68. {
  69. try
  70. {
  71. output.close();
  72. input.close();
  73. client.close();
  74. } // end try
  75. catch(IOException ioException)
  76. {
  77. ioException.printStackTrace();
  78. } // end catch
  79. } //closeConnection
  80.  
  81. public void sendData(String data)
  82. {
  83. try
  84. {
  85. output.writeObject(data);
  86. output.flush();
  87. } // end try
  88. catch(IOException ioException)
  89. {
  90. System.out.print("\nError writing object");
  91. } // end catch
  92. } // end sendData
  93. } // end client
The main method of Client class
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. public class newMain
  3. {
  4. public static void main(String[] args) throws IOException
  5. {
  6. Client c = new Client();
  7. c.connect();
  8. c.streams();
  9. c.processConnection();
  10. c.sendData("here you go");
  11. } // end main
  12.  
  13. } // end newMain
cwl157 is offline   Reply With Quote
Old Apr 20th, 2008, 1:23 PM   #22
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

ok I think I got it. It is a little rough around the edges but the clients take turns entering a string at the keyboard and then it sends it to the server and the other client also receives it. I have questions about the loops though. Since there are only 2 players can the connections be made before the main loop. So in the Server class can the s.accept() call be made outside of the loop twice for the 2 clients? Also in the NewMain class can the client.connect() and client.streams() methods be called before the loop or do they need to be done every time? Right now the clients take turns which i guess works since battleship is a turn based game anyway when its one persons turn the other will not be sending anything.
Server class
java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. //import java.net.*;
  5.  
  6. public class Server
  7. {
  8. private static Socket connection;
  9. private static ObjectOutputStream output;
  10. private static ObjectInputStream input;
  11.  
  12. public static void main(String[] args) throws java.io.IOException
  13. {
  14. ServerSocket s = new ServerSocket(1234);
  15. String string = "";
  16. //connection = s.accept();
  17. while(true)
  18. {
  19. connection = s.accept();
  20. System.out.println("client connected");
  21. streams();
  22. //send("Send this to client");
  23. send(string);
  24. string = receive();
  25. //send(string);
  26. //System.out.println("Listenning...");
  27. //(new ServerThread(s.accept())).start();
  28. //System.out.println("Spawned Thread.");
  29. } // end while
  30. } // end main
  31.  
  32. // establish the streams
  33. private static void streams() throws IOException
  34. {
  35. output=new ObjectOutputStream(connection.getOutputStream());
  36. output.flush();
  37. input=new ObjectInputStream(connection.getInputStream());
  38. } // end streams
  39.  
  40. private static void send(String or_variable)
  41. {
  42. try
  43. {
  44. output.writeObject("String" + or_variable);
  45. output.flush();
  46. } // end try
  47. catch(IOException ioException)
  48. {
  49. }
  50. } // end send
  51.  
  52. private static String receive() throws IOException
  53. {
  54. String test = "";
  55. try
  56. {
  57. test = (String)input.readObject();
  58. System.out.print("\n" + test);
  59. } // end try
  60. catch(ClassNotFoundException classNotFoundException)
  61. {
  62. System.out.print("\nUnknown object recieved");
  63. } // end catch
  64. return test;
  65. } // end receive
  66. } // end Server
Client class
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class Client
  6. {
  7.  
  8. private Socket client;
  9. private ObjectOutputStream output;
  10. private ObjectInputStream input;
  11.  
  12. //Client constructor
  13. private void client()
  14. {
  15. try
  16. {
  17. connect();
  18. streams();
  19. processConnection();
  20. } // end try
  21. catch (EOFException eofException)
  22. {
  23. System.err.println("Client terminated connection");
  24. } // end catch
  25. catch (IOException ioException)
  26. {
  27. ioException.printStackTrace();
  28. } // end catch
  29. finally
  30. {
  31. closeConnection();
  32. } // end finally
  33. } // end client
  34.  
  35. public void connect() throws IOException
  36. {
  37. System.out.print("Attempting Connection\n");
  38. client = new Socket(InetAddress.getByName("192.168.1.102"), 1234);
  39. System.out.print("Connected to: " + client.getInetAddress().getHostName());
  40. } // end connect
  41.  
  42. public void streams() throws IOException
  43. {
  44. output=new ObjectOutputStream(client.getOutputStream());
  45. output.flush();
  46. input=new ObjectInputStream(client.getInputStream());
  47. System.out.print("\nStreams Estabolished\n");
  48. } // end streams
  49.  
  50. public void processConnection() throws IOException
  51. {
  52. String test = "";
  53. // do
  54. // {
  55. try
  56. {
  57. test = (String)input.readObject();
  58. System.out.print("\n" + test);
  59. } // end try
  60. catch(ClassNotFoundException classNotFoundException)
  61. {
  62. System.out.print("\nUnknown object recieved");
  63. } // end catch
  64. // }while(!test.equals("Server Connection Lost")); // end do while
  65. } // end processConnection
  66.  
  67. public void closeConnection()
  68. {
  69. try
  70. {
  71. output.close();
  72. input.close();
  73. client.close();
  74. } // end try
  75. catch(IOException ioException)
  76. {
  77. ioException.printStackTrace();
  78. } // end catch
  79. } //closeConnection
  80.  
  81. public void sendData(String data)
  82. {
  83. try
  84. {
  85. output.writeObject(data);
  86. output.flush();
  87. } // end try
  88. catch(IOException ioException)
  89. {
  90. System.out.print("\nError writing object");
  91. } // end catch
  92. } // end sendData
  93.  
  94. public static String promptUser() throws IOException
  95. {
  96. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  97. String line = null;
  98.  
  99. System.out.print("Enter something: ");
  100. try
  101. {
  102. line = keyInput.readLine();
  103. } // end try
  104. //catches bad user input and throws exception
  105. catch (NumberFormatException ex)
  106. {
  107. System.out.print("Error bad data: ");
  108. } // end catch
  109.  
  110. return line;
  111. } // promptUser
  112. } // end client
NewMain
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. public class newMain
  3. {
  4. public static void main(String[] args) throws IOException
  5. {
  6. Client c = new Client();
  7. while (true)
  8. {
  9. c.connect();
  10. c.streams();
  11. c.processConnection();
  12. String line = c.promptUser();
  13. System.out.println(line);
  14. c.sendData(line);
  15. }
  16. } // end main
  17.  
  18. } // end newMain
cwl157 is offline   Reply With Quote
Old Apr 20th, 2008, 4:35 PM   #23
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 BinarySurfer is on a distinguished road
Re: online game

You raise a good question about the multiple clients connecting at once and sending data simultaneously. I didn't have not tried working with several clients connecting at once and share their information through the server.
I don't have a solid answer right now. Finals are over this Thursday, then I can actually help you or one of the big guys/gals here can solve this one.
BinarySurfer is offline   Reply With Quote
Old Apr 20th, 2008, 5:51 PM   #24
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

well i think what i have works because i connect 2 clients and then whatever i type into 1 client's keyboard input gets printed on the other clients screen. I think that is really all i need. The string is passed from one client to the other via the Server. I am just wondering the best way to organize it with my battleship game because it would be better i think if there was a way where the clients would only connect once and the streams would only be established once. It would be less work but i have taken those methods out of the loops and have not gotten it to work right. I guess if the connections have to be made and streams created each time then it wouldn't be the end of the world. I also need to figure out how the put the Client NewMain into a method because then i would just use that method on the input the user enters on the keyboard and that would be sent to the client and used in the player's takeTurn method in the battleship game.
cwl157 is offline   Reply With Quote
Old Apr 20th, 2008, 8:01 PM   #25
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

I added it to the Player class in my battleship game and it does send the numbers to the other client but I do not think all of them. I think this can be fixed by changing when they are prompted and stuff. Also, it applies what the player enters to itself instead of the other player. And the third thought i have is if both players have a ship at the same location would it be possible to hit your own ship then as well as the other player's ship? Here is my Player class.
java Syntax (Toggle Plain Text)
  1. import ship.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. class Player
  7. {
  8. private Socket client;
  9. private ObjectOutputStream output;
  10. private ObjectInputStream input;
  11. String name;
  12. public int counter = 0;
  13.  
  14. /** Creates a new instance of Player */
  15. public Player(String newName)
  16. {
  17. this.name = newName;
  18. } // end Player
  19.  
  20. // requirements for setting a ship are:
  21. // all spaces have to euqal 'w'
  22. // and can not run off the board
  23. boolean canSetShip(int spacesNeeded, int startRow, int startCol, int direction, Board board)
  24. {
  25. int spacesNeededOne = 0;
  26.  
  27. try
  28. {
  29. // place ship horizontal
  30. if (direction == 0)
  31. {
  32. if (startCol + spacesNeeded > 9)
  33. return false;
  34. else
  35. {
  36. for (int i = startCol; i < startCol + spacesNeeded; i++)
  37. {
  38. if (board.ar[startRow][i] != 'w')
  39. return false;
  40. } // end for
  41. } // end else
  42. } // end if
  43.  
  44. // place ship verticle
  45. else if (direction == 1)
  46. {
  47. if (startRow + spacesNeeded > 9)
  48. return false;
  49. else
  50. {
  51. for (int i = startRow; i < startRow + spacesNeeded; i++)
  52. {
  53. if (board.ar[i][startCol] != 'w')
  54. return false;
  55. } // end for
  56. } // end else
  57. } // end else if
  58. } // end try
  59. catch(ArrayIndexOutOfBoundsException e)
  60. {
  61. System.out.println("ArrayIndexOutOfBoundsException thrown");
  62. return false;
  63. } // end catch
  64. return true;
  65. } // end canSetShip
  66.  
  67. void setSub(Board b, Sub s)
  68. {
  69. // find a random row
  70. Random startRowRand = new Random();
  71. int startRow = startRowRand.nextInt(10);
  72.  
  73. // find a random col
  74. Random startColRand = new Random();
  75. int startCol = startColRand.nextInt(10);
  76.  
  77. // find a random direction
  78. Random directionRand = new Random();
  79. int direction = directionRand.nextInt(2);
  80.  
  81. int counter = 0;
  82. boolean isSet = false;
  83.  
  84. while (!isSet)
  85. {
  86. if (canSetShip(s.getSize(), startRow, startCol, direction, b))
  87. {
  88. // place ship horizontally
  89. if (direction == 0)
  90. {
  91. for (int i = startCol; counter < s.getSize(); i++)
  92. {
  93. b.ar[startRow][i] = 'S';
  94. counter++;
  95. } // end for
  96. } // end if
  97. // place ship vertically
  98. else if (direction == 1)
  99. {
  100. for (int i = startRow; counter < s.getSize(); i++)
  101. {
  102. b.ar[i][startCol] = 'S';
  103. counter++;
  104. } // end for
  105. } // end else if
  106. isSet = true;
  107. } // end if
  108. else
  109. {
  110. direction = directionRand.nextInt(2);
  111. startRow = startRowRand.nextInt(10);
  112. startCol = startColRand.nextInt(10);
  113. } // end else
  114. } // end while
  115. } // end setSub
  116.  
  117. void setBattleShip(Board b, BattleShip bS)
  118. {
  119. // find a random row
  120. Random startRowRand = new Random();
  121. int startRow = startRowRand.nextInt(10);
  122.  
  123. // find a random col
  124. Random startColRand = new Random();
  125. int startCol = startColRand.nextInt(10);
  126.  
  127. // find a random direciton
  128. Random directionRand = new Random();
  129. int direction = directionRand.nextInt(2);
  130.  
  131. int counter = 0;
  132. boolean isSet = false;
  133.  
  134. while (!isSet)
  135. {
  136. if (canSetShip(bS.getSize(), startRow, startCol, direction, b))
  137. {
  138. // place ship horizontally
  139. if (direction == 0)
  140. {
  141. for (int i = startCol; counter < bS.getSize(); i++)
  142. {
  143. b.ar[startRow][i] = 'B';
  144. counter++;
  145. } // end for
  146. } // end if
  147. // place ship vertically
  148. else if (direction == 1)
  149. {
  150. for (int i = startRow; counter < bS.getSize(); i++)
  151. {
  152. b.ar[i][startCol] = 'B';
  153. counter++;
  154. } // end for
  155. } // end else if
  156. isSet = true;
  157. } // end if
  158. else
  159. {
  160. direction = directionRand.nextInt(2);
  161. startRow = startRowRand.nextInt(10);
  162. startCol = startColRand.nextInt(10);
  163. } // end else
  164. } // end while
  165. } // end setBattleShip
  166.  
  167.  
  168. void setPatrol(Board b, Patrol p)
  169. {
  170. // find a random row
  171. Random startRowRand = new Random();
  172. int startRow = startRowRand.nextInt(10);
  173.  
  174. // find a random col
  175. Random startColRand = new Random();
  176. int startCol = startColRand.nextInt(10);
  177.  
  178. // find a random direction
  179. Random directionRand = new Random();
  180. int direction = directionRand.nextInt(2);
  181.  
  182. int counter = 0;
  183. boolean isSet = false;
  184.  
  185. while (!isSet)
  186. {
  187. if (canSetShip(p.getSize(), startRow, startCol, direction, b))
  188. {
  189. // place ship horizontally
  190. if (direction == 0)
  191. {
  192. for (int i = startCol; counter < p.getSize(); i++)
  193. {
  194. b.ar[startRow][i] = 'P';
  195. counter++;
  196. } // end for
  197. } // end if
  198. // place ship vertically
  199. else if (direction == 1)
  200. {
  201. for (int i = startRow; counter < p.getSize(); i++)
  202. {
  203. b.ar[i][startCol] = 'P';
  204. counter++;
  205. } // end for
  206. } // end else if
  207. isSet = true;
  208. } // end if
  209. else
  210. {
  211. direction = directionRand.nextInt(2);
  212. startRow = startRowRand.nextInt(10);
  213. startCol = startColRand.nextInt(10);
  214. } // end else
  215. }