Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. //MAIN SERVER APP - CAN RUN AS A THREAD (and stop/start etc.)
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. public class ServerApp implements Runnable
  7. {
  8. volatile boolean stopped = true;
  9. private ClientTable clientTable;
  10. private ServerSocket serverSocket = null; //open a new socket.
  11. //private Socket socket = null;
  12.  
  13. public void stopServer()
  14. {
  15. //check if server is already stopped
  16. if(stopped)
  17. {
  18. System.out.println("Error: Server is already stopped!");
  19. }
  20. else
  21. {
  22. try
  23. {
  24. //socket.close();
  25. stopped = true;
  26. serverSocket.close();
  27. System.out.println("stopping server");
  28. }
  29. catch (IOException e)
  30. {
  31. System.err.println("Error: Nonexistent socket. - the programs fucked.");
  32. }
  33. }
  34. }
  35.  
  36. public void startServer()
  37. {
  38. //check if server is already running
  39. if (!stopped)
  40. {
  41. System.out.println("Error: Server is already running!");
  42. }
  43. else
  44. {
  45. //stopped is false i.e. server is now running
  46. stopped = false;
  47. System.out.println("starting server");
  48. //each time we start the server we need to create a NEW thread - you can't resume old ones lol
  49. Thread thread = new Thread(this);
  50. //then launch the new thread.
  51. thread.start();
  52. }
  53. }
  54.  
  55. public ClientTable getClientTable()
  56. {
  57. return clientTable;
  58. }
  59.  
  60. public void run()
  61. {
  62. stopped=false;
  63. while(!stopped)
  64. {
  65. try
  66. { //INIT SERVER CODE:
  67. ClientTable clientTable = new ClientTable(); //sharing by server threads
  68. //ServerSocket serverSocket = null; //Opening a new socket
  69.  
  70. try
  71. {
  72. serverSocket = new ServerSocket(Port.number); //trying with a specific port - this is separate to the other socket mentioned below.
  73. }
  74. catch(IOException e)
  75. {
  76. System.err.println("Couldn't listen on port "+Port.number); //if port fails
  77. //TODO: exit here!
  78. }
  79. //MAIN SERVER CODE:
  80. try
  81. {
  82. //LOOP - Forever, common with servers.
  83. while(!stopped)
  84. {
  85. //still gotta check those interrupts to see if we killed the server.
  86. try
  87. {
  88. //System.out.println("Running.");
  89. Thread.sleep(1000);
  90. Socket socket = serverSocket.accept(); //listen to socket & accept new clients.
  91. BufferedReader fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); //so we can use readLine():
  92. String clientName = fromClient.readLine(); //get client's name
  93. System.out.println("New connection! Client Name: "+clientName); //print new client name
  94. clientTable.add(clientName); //add client to the table
  95. (new ServerReceiver(clientName, fromClient, clientTable)).start(); //create & start new thread to read from client.
  96. PrintStream toClient = new PrintStream(socket.getOutputStream()); //create & start new thread to write to client.
  97. (new ServerSender(clientTable.getQueue(clientName), toClient)).start();
  98. //work out how to return client table.
  99.  
  100.  
  101. //have a fiddle with the server socket timeouts etc.
  102. //every now and again we sleep this to check for an interrupt
  103.  
  104. }
  105. catch (InterruptedException ex)
  106. {
  107. //kills the thread nicely :-)
  108. //System.out.println("caught");
  109.  
  110. Thread.currentThread().interrupt();
  111. }
  112. }
  113. //System.out.println("Server Stopped.");
  114. }
  115. catch(IOException e)
  116. {
  117. System.err.println("IO Error " + e.getMessage());
  118. //TODO: Establish new connection?
  119. }
  120.  
  121.  
  122.  
  123. //every now and again the server sleeps to allow for the code to check for interrupts.
  124. Thread.sleep(1000);
  125. }
  126. catch (InterruptedException ex)
  127. {
  128. //killing the thread nicely :-)
  129. Thread.currentThread().interrupt();
  130. }
  131. }
  132. System.out.println("Server Stopped.");
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement