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