Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2008, 6:38 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
online game

I completed the battleship game that i was working on and posted that other thread with the question but now i have another question. I have made it 2 player so the players enter their name and where they want to place the ships and everything but i want to make it so 2 people can play each other over the internet. I have done a simple echo server in java but i am still confused if i have multiple classes and stuff how i make the server so in the places where now i would display to the screen i want to display to the 2 clients. Would i have the server end run the main battleship program and then just change the display methods to display the board and moves to the clients connected instead of to the screen? I am confused on how i could do that? Thanks for any input. If i have to post code i will but its like 8 classes basically in the board class I have a printBoard() method that prints the board out so that would have to print to the clients connected and i would have to print the prompts and wait for the response from the clients on where to place ships and where to try and hit the opponents ship.
cwl157 is offline   Reply With Quote
Old Apr 15th, 2008, 8:35 PM   #2
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

ok so i looked at the echo server example and i think here is what i want. I want it to be that all the server does is prompt the user for their name, where to place the ships and then when its their turn what row and column to try and attack the other persons ship. It will take that information into the appropiate methods and take the turn or place the ship so right now i have a method that reads from the command line so that would have to read from a Socket instead right? And then the server would send back that players board back to the appropiate client connected and then send to both players if they hit a ship or missed. I have all the logic correct i just seem to be stuck on sending and recieving from a socket to and from the server.
cwl157 is offline   Reply With Quote
Old Apr 16th, 2008, 4:34 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

Ok the more I think about this the more i think that it is simpler then what i am trying to do. Basically if each client has a copy of the battleship program and each connects to the same server then couldn't the server just be used to relay the moves to the other client and that is what would be used as input into the program? So each battleship program would only have 1 player and 1 board and the moves that are made against that player and board come across the server from the second client right?
cwl157 is offline   Reply With Quote
Old Apr 17th, 2008, 8:48 AM   #4
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 53
Rep Power: 0 BinarySurfer is an unknown quantity at this point
Re: online game

Yeah, the client and server revision is much simpler and easier. It sounds like a great plan, but I don't understand the last part, "board come across the server from the second client right?" If the only data being transmitted is each player's moves, then there is no need to transmit the board because each client will interpret the received move accordingly. Like you said, the received data would be the other player's move and the each client would distinguish a win or loss.
BinarySurfer is offline   Reply With Quote
Old Apr 17th, 2008, 9:34 AM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

Sorry, that wording is a little confusing i mean the move would be sent to the server and that move would be done against the player's board. So each client would just get 1 player and 1 board and the move against those boards would be taken from the server. But does that sound like a good plan? I've never done anything like this over the internet before so i dont really know what the best design would be.
cwl157 is offline   Reply With Quote
Old Apr 17th, 2008, 4:49 PM   #6
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

so to start i am just trying to make 2 clients and a server. The clients will connect to the server and pass a message from one client to the other and it is not working right. The message never gets sent and the server and both clients just hang. Do i need a different name for the different client Sockets? If so I don't know how i would do this because both people playing will be running exact copies of the same program and connect to the server and then the server passes messages between them.

Here is the server code i have
java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class Server
  5. {
  6. public static void main(String[] args) throws IOException
  7. {
  8. ServerSocket srvr = new ServerSocket(1234);
  9. Socket skt1 = new Socket();
  10.  
  11. skt1 = srvr.accept();
  12. System.out.print("Server has connected!\n");
  13. skt1 = srvr.accept();
  14. System.out.print("Server has connected!\n");
  15.  
  16. // network IO for client
  17. PrintWriter out1 = new PrintWriter(skt1.getOutputStream(), true);
  18. BufferedReader in1 = new BufferedReader(
  19. new InputStreamReader(
  20. skt1.getInputStream()));
  21. String line = null;
  22. line = in1.readLine();
  23. out1.println(line);
  24.  
  25.  
  26. out1.close();
  27. in1.close();
  28. skt1.close();
  29. } // end main
  30. } // end class Server
here is the client code I have
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class Client {
  5. public static void main(String[] args) throws IOException
  6. {
  7. Socket skt1 = new Socket("Carl1", 1234);
  8. PrintWriter out = new PrintWriter(skt1.getOutputStream(), true);
  9. BufferedReader in = new BufferedReader(new InputStreamReader(
  10. skt1.getInputStream()));
  11.  
  12.  
  13. String line = "Send this to the other client";
  14. while(true)
  15. {
  16. try
  17. {
  18. line = in.readLine();
  19. System.out.println(line);
  20. out.println(line);
  21. } // end try
  22. //catches bad user input and throws exception
  23. catch (NumberFormatException ex)
  24. {
  25. System.out.print("Error bad data: ");
  26. } // end catch
  27. } // end while
  28. } // end main
  29. } // end Client

I want the one client to send the String line to the server and then the server to pass it to the second client. This is like how the battleship game will work one player sends the row and column he wants to hit and the server sends that information to the other client.
cwl157 is offline   Reply With Quote
Old Apr 17th, 2008, 8:11 PM   #7
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

Alright I have an echo server that can connect to clients and echo back what they enter. Now I am stuck on how do i get the input from one client and send it to another client rather than back to the client it came from. I have 3 classes and i will post them. If someone could let me know what i need to do to make it so instead of the server echoing the information it passes the information to a second client.

Sever, main method
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.  
  9. public static void main(String[] args) throws java.io.IOException
  10. {
  11. ServerSocket s = new ServerSocket(1234);
  12.  
  13. while(true)
  14. {
  15. System.out.println("Listenning...");
  16. (new ServerThread(s.accept())).start();
  17. System.out.println("Spawned Thread.");
  18. } // end while
  19. } // end main
  20. } // end Server

ServerThread - created for each new client that connects
java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class ServerThread extends Thread
  5. {
  6. private BufferedReader input = null;
  7. private PrintWriter output = null;
  8. private PrintWriter output1 = null;
  9. private BufferedReader keyInput = null;
  10.  
  11. /** Creates a new instance of MudServerThread */
  12. public ServerThread(Socket sock)
  13. {
  14. try
  15. {
  16. input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  17. output = new PrintWriter(sock.getOutputStream(), true);
  18.  
  19. keyInput = new BufferedReader(new InputStreamReader(System.in));
  20. } // end try
  21. catch(IOException e)
  22. {
  23. System.err.println("Problem with thread!!");
  24. System.exit(1);
  25. } // end catch
  26. } // end ServerThread
  27.  
  28. public void run()
  29. {
  30. String line = "";
  31. while (true)
  32. {
  33. try
  34. {
  35. line = input.readLine();
  36. System.out.println(line);
  37. output.println(line);
  38.  
  39.  
  40. } // end try
  41. catch(IOException e)
  42. {
  43. System.err.println(e);
  44. System.exit(1);
  45. } // end catch
  46. } // end while
  47. } // end run
  48. } // end class ServerThread

the 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 17th, 2008, 11:49 PM   #8
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: online game

I want to help you out. I wrote a program in college which might get you started. I need to go through all the source and make sure that none of it is copyrighted. If it is all written by me (none provided by my professor) then I will send it to you. It is too late to go through it tonight. It seems like you are on the right track so far though.
mbd is offline   Reply With Quote
Old Apr 18th, 2008, 5:15 AM   #9
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 53
Rep Power: 0 BinarySurfer is an unknown quantity at this point
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
Old Apr 18th, 2008, 2:20 PM   #10
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: online game

hmm, im not sure about this as i am only just beginning Java so im not sure how it would handle something like what im about to suggest.

When a new client connects to the server a connection is established and sotred. Then it is passed to a new Thread and all data from that client is handled by that thread.

Now if you store that connection in a global array, if this is possible in Java. Then it becomes avaliable to everything. This means that other threads that are running for other clients can access the connection via the global array and send data to it.

Cheers,
Chris
__________________
Steven Skiena - Algorithms
Freaky Chris is online now   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
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programmers wanted for an online text based game esimagin Paid Job Offers 1 May 18th, 2006 5:13 PM
Conflict Online (Browser Game) JavaMan Existing Project Development 7 Apr 28th, 2006 12:02 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM
Programmers Wanted: Online FPS game troy_eisert C++ 0 Feb 2nd, 2005 10:59 PM
Programmers Needed! Online Game troy_eisert C++ 2 Jan 29th, 2005 12:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC