Thread: online game
View Single Post
Old Apr 20th, 2008, 1:23 PM   #22
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
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