Thread: online game
View Single Post
Old Apr 19th, 2008, 10:42 AM   #13
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 327
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

ok, so i changed the client and created a class that has the main method where i create a client but nothing happened. I would run the client and i never got any exceptions or messages it would just quit. I changed the sendData method to public and passed it a string but when i do that and run the client i get a nullPointerException. It occurs in the sendData method at the output.writeObject(data) line and in main at the line where i tell it to send the data. I'll post the client and server again.

Server
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.  
  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. } // end Server
client
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. private 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. private 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. private 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. private 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. public void sendData(String data)
  81. {
  82. try
  83. {
  84. output.writeObject(data);
  85. output.flush();
  86. } // end try
  87. catch(IOException ioException)
  88. {
  89. System.out.print("\nError writing object");
  90. } // end catch
  91. } // end sendData
  92. } // end client
newMain, this is the main method for the Client
java Syntax (Toggle Plain Text)
  1. public class newMain
  2. {
  3. public static void main(String[] args)
  4. {
  5. Client c = new Client();
  6. c.sendData("here you go");
  7. } // end main
  8.  
  9. } // end newMain
cwl157 is offline   Reply With Quote