Advertisement
Guest User

Untitled

a guest
Nov 4th, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Server.java*/
  2. import java.io.*; //importing io classes
  3. import java.net.*; //importing networking classes
  4. class Server {
  5. private static Client clients[]; //array storing clients
  6. private static Client clientsTmp[];
  7. private static int noOfClients = 0;
  8. private static ServerSocket server;
  9. private static Socket client;
  10. private static Sender sender;
  11. private static PrintWriter outStream;
  12. private static BufferedReader buffer;
  13. private static String keyboardInput;
  14. public static void main(String argc[]) {
  15. try {
  16. server = new ServerSocket(2000);
  17. sender = new Sender();
  18. while (true) {
  19. System.out.println("New client connected...\nClients connected - " + noOfClients);
  20. client = server.accept();
  21. addClient(client);
  22. }
  23. } catch (Exception e) {
  24. System.out.println("Could not create server..." + e);
  25. System.exit(-1);
  26. }
  27. }
  28. static void addClient(Socket client) { //adds new client
  29. try {
  30. clientsTmp = new Client[++noOfClients];
  31. int i = 0;
  32. while (i < noOfClients - 1) {
  33. clientsTmp[i] = clients[i];
  34. i++;
  35. }
  36. clientsTmp[noOfClients - 1] = new Client(client);
  37. clients = clientsTmp.clone();
  38. } catch (Exception e) {
  39. System.out.println("Some problem occured " + e);
  40. }
  41. }
  42. static class Client {
  43. Socket client;
  44. PrintWriter outputStream;
  45. Client(Socket clientSocket) {
  46. client = clientSocket;
  47. try {
  48. outputStream = new PrintWriter(client.getOutputStream());
  49. } catch (Exception e) {
  50. System.out.println("Could not initialise client output stream");
  51. }
  52. }
  53. void print(String str) {
  54. try {
  55. outputStream.println(str);
  56. outputStream.flush();
  57. System.out.println("Sent");
  58. } catch (Exception e) {
  59. System.out.println("Could not send to client" + e);
  60. }
  61.  
  62. }
  63. void close() {
  64. try {
  65. client.close();
  66. outputStream.close();
  67. } catch (Exception e) {
  68. System.out.println("Could not close client");
  69. }
  70. }
  71. }
  72. static class Sender extends Thread {
  73. Sender() {
  74. super("Sender Thread");
  75. try {
  76. buffer = new BufferedReader(new InputStreamReader(System. in ));
  77. System.out.println("Starting broadcast...");
  78. start();
  79. } catch (Exception e) {
  80. System.out.println("Could not start sender thread");
  81. System.exit(-1);
  82. }
  83. }
  84. public void run() {
  85. try {
  86. int i = 0;
  87. while (!(keyboardInput = buffer.readLine()).equals("stop")) {
  88. while (i < noOfClients) {
  89. System.out.println("Sending to client " + i);
  90. clients[i++].print(keyboardInput);
  91. }
  92. System.out.println("Packets containing " + keyboardInput + "sent.Waiting for next broadcast...");
  93. }
  94. } catch (Exception e) {
  95. System.out.println("Could not send");
  96. System.exit(-1);
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement