Programming Forums
User Name Password Register
 

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

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

BinarySurfer, I have followed your suggestions but it just hangs now. I added those methods to the Server class and i assume that the ServerThread class isn't even used anymore right and that the client stays the same?

Here is the code i have after making those changes.
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.  
  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 class
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class Client {
  6. public static void main(String[] args) throws IOException
  7. {
  8. Socket skt1 = new Socket("192.168.1.102", 1234);
  9. PrintWriter out = new PrintWriter(skt1.getOutputStream(), true);
  10. BufferedReader in = new BufferedReader(new InputStreamReader(
  11. skt1.getInputStream()));
  12. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14. Random r = new Random();
  15. int[] ar = new int[10];
  16.  
  17. for (int i = 0; i < 10; i++)
  18. ar[i] = r.nextInt(10);
  19.  
  20. String line = "Send this to the other client";
  21. while(true)
  22. {
  23. try
  24. {
  25. // line = in.readLine();
  26. System.out.println("Enter something");
  27. out.println(keyInput.readLine());
  28.  
  29. System.out.println(in.readLine());
  30. //out.println(line);
  31. for (int i = 0; i < 10; i++)
  32. System.out.print(ar[i] + " ");
  33.  
  34. System.out.println();
  35.  
  36. } // end try
  37. //catches bad user input and throws exception
  38. catch (NumberFormatException ex)
  39. {
  40. System.out.print("Error bad data: ");
  41. } // end catch
  42. } // end while
  43. } // end main
  44. } // end Client
cwl157 is offline   Reply With Quote
Old Apr 19th, 2008, 9:50 AM   #12
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 BinarySurfer is on a distinguished road
Re: online game

@Freaky Chris
I've never done it that way. A better way might be to make the socket public or the send method public. Then, in the other applications, you can call the class that has the connection and use the connection or call the send class.
java Syntax (Toggle Plain Text)
  1. ServerClass.send("something");
I could be wrong because I have not tested it to know for myself. I have only used that accessed variable from one class to other classes, but I think the logic would work.

@cwl157
Sorry, I only got to send you the sever class, but the server looks right. The client does have to be redesigned. Not implementing what you need, here's the bare bones of the client
java Syntax (Toggle Plain Text)
  1. private Socket client
  2. private ObjectOutputStream output;
  3. private ObjectInputStream input;
  4.  
  5. private void client(){
  6. try{
  7. connect();
  8. streams();
  9. processConnection();
  10. }
  11. catch (EOFException eofException){
  12. System.err.println("Client terminated connection");
  13. }
  14. catch (IOException ioException){
  15. ioException.printStackTrace();
  16. }
  17. finally{
  18. closeConnection();
  19. }
  20. }
  21. private void connect() throws IOException{
  22. System.out.print("Attempting Connection\n");
  23. client = new Socket(InetAddress.getByName(192.168.1.102), 1234);
  24. System.out.print("Connected to: " + client.getInetAddress().getHostName());
  25. }
  26. private void streams() throws IOException{
  27. output=new ObjectOutputStream(client.getOutputStream());
  28. output.flush();
  29. input=new ObjectInputStream(client.getInputStream());
  30. System.out.print("\nStreams Estabolished\n");
  31. }
  32. private void processConnection() throws IOException{
  33. String test = ""
  34. do{
  35. try{
  36. test = (String)input.readObject();
  37. System.out.print("\n" + test);
  38. }
  39. catch(ClassNotFoundException classNotFoundException){
  40. System.out.print("\nUnknown object recieved");
  41. }
  42. }while(!test.equals("Server Connection Lost"));
  43. }
  44. private void closeConnection(){
  45. try{
  46. output.close();
  47. input.close();
  48. client.close();
  49. }
  50. catch(IOException ioException){
  51. ioException.printStackTrace();
  52. }
  53. }
  54. private void sendData(String data){
  55. try{
  56. output.writeObject(data);
  57. output.flush();
  58. }
  59. catch(IOException ioException){
  60. System.out.print("\nError writing object");
  61. }
  62. }
That should do it.

Last edited by BinarySurfer; Apr 19th, 2008 at 9:52 AM. Reason: Forgot to inicialize 2 objects
BinarySurfer is offline   Reply With Quote
Old Apr 19th, 2008, 10:42 AM   #13
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
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
Old Apr 19th, 2008, 1:08 PM   #14
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: online game

BinarySurfer, i thought about making the socket public but i was thinging about the fact that if multiple peopel connect to the server then you have the issue of all the connection sockets being named the same, which of course is not going to be helpful.

However creating a class would work well, cause it would allow you to store other information about that client as well which could prove helpful. Just then remains on knowiing which class you want to check for the connection.

As i know if you use a couple of arrays then they can link niceley bassed of one thing such as a user name and from that you can then retrieive the IP from another array. But i guess you could implement something very similar with classes.

Cheers,
Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old Apr 19th, 2008, 1:19 PM   #15
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 BinarySurfer is on a distinguished road
Re: online game

Sorry, I think you go what I said to FreakyChris mixed in, but if you want to keep the client class separated from the game class, that's fine. In the newMain class, you have the idea down, but the problem is the Client class never attempted to established a connection with the server, which is why there is no error or output and it quits. Keeping it the way you have it, change the newMain as follows:
java Syntax (Toggle Plain Text)
  1. ...
  2.  
  3. Client c = new Client();
  4. c.connect();
  5. c.streams().
  6. c.processConnection();
  7. c.sendData("here you go");
  8.  
  9. ...
In the newMain, Client never ran though the needed methods that establish the connection or anything. Now that the needed methods are called, it should connect and what not.
Now the server and client should connect and communicate.

Last edited by BinarySurfer; Apr 19th, 2008 at 1:20 PM. Reason: Needed more dots :P
BinarySurfer is offline   Reply With Quote
Old Apr 19th, 2008, 4:59 PM   #16
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

I am really confused. It does print out to all the clients a string that i enter on the server. Now how do i make it so the client can send Strings to the server and then the Server print them on all clients? I think that is the next step. That way in my game I can have the person enter their move via keyboard input and then pass it to the server which would then print it out on all clients so both clients could see all the moves. Then when there is a winner I could send that to all clients and have it displayed as well. So basically I need a method that takes a string and sends it to the server and then the server passes it to all clients which would be max 2 clients because 2 people play at once. Here is the Server class's receive method that I have been working on but does not work. I thought if this could return what is received as a String then that String could be passed into the send method and sent to all the clients. I just hangs when receive is hit.

java Syntax (Toggle Plain Text)
  1. private static String receive() throws IOException
  2. {
  3. String test = "";
  4. try
  5. {
  6. test = (String)input.readObject();
  7. System.out.print("\n" + test);
  8. } // end try
  9. catch(ClassNotFoundException classNotFoundException)
  10. {
  11. System.out.print("\nUnknown object recieved");
  12. } // end catch
  13. return test;
  14. } // end receive

Last edited by cwl157; Apr 19th, 2008 at 5:28 PM.
cwl157 is offline   Reply With Quote
Old Apr 20th, 2008, 7:06 AM   #17
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 BinarySurfer is on a distinguished road
Re: online game

The server and client are connected and chatting?
Quote:
Originally Posted by cwl157 View Post
Here is the Server class's receive method that I have been working on but does not work. I thought if this could return what is received as a String then that String could be passed into the send method and sent to all the clients. I just hangs when receive is hit.

java Syntax (Toggle Plain Text)
  1. private static String receive() throws IOException
  2. {
  3. String test = "";
  4. try
  5. {
  6. test = (String)input.readObject();
  7. System.out.print("\n" + test);
  8. } // end try
  9. catch(ClassNotFoundException classNotFoundException)
  10. {
  11. System.out.print("\nUnknown object recieved");
  12. } // end catch
  13. return test;
  14. } // end receive
Well, that method is where the server accepts incoming data. Yeah, test is a lousy name now that I think of it.
For sending data, for both the client and the server, why don't you use the send method? When ever you want to send data from either, you just call the method with the info as an argument.
java Syntax (Toggle Plain Text)
  1. public Client c
  2.  
  3. c = new Client();
  4. c.connect();
  5. c.streams().
  6. c.processConnection();
  7. ... a lot of code later ...
  8. c.sendData("5, 8");
You can put the sendData call in the same method that gets the input from the user, so the data is sent to the server and send back and to the other client.
BinarySurfer is offline   Reply With Quote
Old Apr 20th, 2008, 11:08 AM   #18
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

the receive method i posted does not work it just hangs waiting for something when its called. Also, I can just call the client's sendData method from the Server class? When i send data from the client to the server the Server never receives it. When i call sendData from the client the Server never gets it. So i thought i would need a receive method in the Server class. The send method of the Server sends the string just fine to the Client.
cwl157 is offline   Reply With Quote
Old Apr 20th, 2008, 11:21 AM   #19
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 BinarySurfer is on a distinguished road
Re: online game

Time's getting a little tight here now that finals are around the corner, but post your client-server classes and I know this is a stupid question, but is your firewall blocking the connection? Your Windows firewall too?
BinarySurfer is offline   Reply With Quote
Old Apr 20th, 2008, 12:28 PM   #20
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
Re: online game

Thats not a stupid question, my Client-Server program was originally being blocked by a firewall as well.
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools