Thread: online game
View Single Post
Old Apr 18th, 2008, 5:15 AM   #9
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 BinarySurfer is on a distinguished road
Re: online game

I would use Socket and ServerSocket for the server class. In the first group of code, your
java Syntax (Toggle Plain Text)
  1. ServerSocket s = new ServerSocket(1234);
is fine. To make the process easier for me, I globalized the Socket for the class
java Syntax (Toggle Plain Text)
  1. class Server{
  2. private Socket connection;
  3. private ObjectOutputStream output;
  4. private ObjectInputStream input;
Then in a while loop, have the server connection listening
java Syntax (Toggle Plain Text)
  1. connection = server.accept();
You also want have, in the loop, a call to a method that establishes the streams and throws IOException
java Syntax (Toggle Plain Text)
  1. private void streams() throws IOException{
  2. output=new ObjectOutputStream(connection.getOutputStream());
  3. output.flush();
  4. input=new ObjectInputStream(connection.getInputStream());
  5. }
Now, for sending data (I have only done strings) you want to create a method.
java Syntax (Toggle Plain Text)
  1. private void send(String or_variable){
  2. try{
  3. output.writeObject("String" + or_variable);
  4. output.flush();
  5. }
  6. catch(IOException ioException){
  7. }
  8. }
  9. }
BinarySurfer is offline   Reply With Quote