Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. // This file contains material supporting section 3.7 of the textbook:
  2. // "Object Oriented Software Engineering" and is issued under the open-source
  3. // license found at www.lloseng.com
  4.  
  5. import java.io.*;
  6. import ocsf.server.*;
  7.  
  8. /**
  9. * This class overrides some of the methods in the abstract
  10. * superclass in order to give more functionality to the server.
  11. *
  12. * @author Dr Timothy C. Lethbridge
  13. * @author Dr Robert Laganière
  14. * @author François Bélanger
  15. * @author Paul Holden
  16. * @version July 2000
  17. */
  18. public class EchoServer extends AbstractServer
  19. {
  20. //Class variables *************************************************
  21.  
  22. /**
  23. * The default port to listen on.
  24. */
  25. final public static int DEFAULT_PORT = 5555;
  26.  
  27. //Constructors ****************************************************
  28.  
  29. /**
  30. * Constructs an instance of the echo server.
  31. *
  32. * @param port The port number to connect on.
  33. */
  34. public EchoServer(int port)
  35. {
  36. super(port);
  37. }
  38.  
  39.  
  40. //Instance methods ************************************************
  41.  
  42. /**
  43. * This method handles any messages received from the client.
  44. *
  45. * @param msg The message received from the client.
  46. * @param client The connection from which the message originated.
  47. */
  48. public void handleMessageFromClient
  49. (Object msg, ConnectionToClient client)
  50. {
  51. System.out.println("Message received: " + msg + " from " + client);
  52. this.sendToAllClients(msg);
  53. }
  54.  
  55. /**
  56. * This method overrides the one in the superclass. Called
  57. * when the server starts listening for connections.
  58. */
  59. protected void serverStarted()
  60. {
  61. System.out.println
  62. ("Server listening for connections on port " + getPort());
  63. }
  64.  
  65. /**
  66. * This method overrides the one in the superclass. Called
  67. * when the server stops listening for connections.
  68. */
  69. protected void serverStopped()
  70. {
  71. System.out.println
  72. ("Server has stopped listening for connections.");
  73. }
  74.  
  75.  
  76. //Agoose added this
  77. protected void clientConnected(ConnectionToClient client) {
  78. System.out.println
  79. ("A client has connected ");
  80. }
  81.  
  82. synchronized protected void clientDisconnected(ConnectionToClient client) {
  83. System.out.println
  84. ("A client has disconnected");
  85. }
  86. synchronized protected void clientException(
  87. ConnectionToClient client, Throwable exception) {
  88. super.clientException(client, exception);
  89. try {
  90. client.close();
  91. }catch(IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95.  
  96.  
  97.  
  98.  
  99. //Class methods ***************************************************
  100.  
  101. /**
  102. * This method is responsible for the creation of
  103. * the server instance (there is no UI in this phase).
  104. *
  105. * @param args[0] The port number to listen on. Defaults to 5555
  106. * if no argument is entered.
  107. */
  108. public static void main(String[] args)
  109. {
  110. int port = 0; //Port to listen on
  111.  
  112. try
  113. {
  114. port = Integer.parseInt(args[0]); //Get port from command line
  115. }
  116. catch(Throwable t)
  117. {
  118. port = DEFAULT_PORT; //Set port to 5555
  119. }
  120.  
  121. EchoServer sv = new EchoServer(port);
  122.  
  123. try
  124. {
  125. sv.listen(); //Start listening for connections
  126. }
  127. catch (Exception ex)
  128. {
  129. System.out.println("ERROR - Could not listen for clients!");
  130. }
  131. }
  132. }
  133. //End of EchoServer class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement