Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. // Implement a simple MUSH client in one class, including a static main() that
  5. // instantiates the class
  6. public class MUSHClient {
  7. // The internet hostname of the MUSH to connect to
  8. private String hostname;
  9.  
  10. // The port on the server where the MUSH is located
  11. private int port;
  12.  
  13. // The IP address of the server; we look it up early on in the object's life
  14. private InetAddress ip_address;
  15.  
  16. // A socket that will be connected to the remove host
  17. private Socket socket;
  18.  
  19. // For buffered reading of data from the socket
  20. private BufferedReader socket_input;
  21.  
  22. // For writing data to the socket
  23. private PrintWriter socket_output;
  24.  
  25. // Remember whether we are connected or not
  26. private boolean connected;
  27.  
  28. // This is how the client knows it is time to quit the main loop
  29. private boolean run_client;
  30.  
  31. // A sample driver for the client, although it could easily be instantiated
  32. // from other code
  33. public static void main(String[] args) {
  34. String hostname;
  35. int port;
  36. BufferedReader in;
  37. MUSHClient client;
  38.  
  39. client = null;
  40. in = new BufferedReader(new InputStreamReader(System.in));
  41.  
  42. // First, read in the hostname and port to connect to
  43. try {
  44. System.out.print("Hostname: ");
  45. hostname = in.readLine();
  46. System.out.print("Port: ");
  47. port = Integer.parseInt(in.readLine());
  48. } catch (Exception e) {
  49. System.out.println("Exception: " + e);
  50. return;
  51. }
  52.  
  53. // Second, instantiate the client, connect it to the MUSH, and run the
  54. // main loop
  55. try {
  56. client = new MUSHClient(hostname, port);
  57. client.connect();
  58. client.run();
  59. } catch (Exception e) {
  60. System.out.println("Exception: " + e);
  61. } finally {
  62. // Clean up by disconnecting from the MUSH
  63. try {
  64. if (client != null && client.connected())
  65. client.shutdown();
  66. } catch (Exception e) {
  67. System.out.println("Exception: " + e);
  68. }
  69. }
  70. }
  71.  
  72. // Constructor; takes a hostname and port to connect to and performs the
  73. // DNS lookup
  74. public MUSHClient(String hostname, int port) throws UnknownHostException {
  75. this.hostname = hostname;
  76. this.port = port;
  77. ip_address = InetAddress.getByName(hostname);
  78. connected = false;
  79. }
  80.  
  81. // Return whether the client is currently in a connected state
  82. public boolean connected() {
  83. return connected;
  84. }
  85.  
  86. // Connect to the MUSH by creating a socket connection and the appropriate
  87. // reader and writer objects, then setting the connected attribute
  88. public void connect() throws IOException {
  89. socket = new Socket(ip_address, port);
  90. socket_input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  91. socket_output = new PrintWriter(socket.getOutputStream(), true);
  92. connected = true;
  93. }
  94.  
  95. // Handle a command from the user, which currently should be either /quit or
  96. // a line of text to pass on to the MUSH
  97. public void handle_command(String command) {
  98. if (command.equals("/quit")) {
  99. run_client = false;
  100. return;
  101. }
  102.  
  103. socket_output.println(command);
  104. }
  105.  
  106. // Run the main loop until the user types /quit
  107. public void run() throws IOException {
  108. BufferedReader console_input = new BufferedReader(new InputStreamReader(System.in));
  109.  
  110. System.out.println("Type /quit to quit.");
  111.  
  112. run_client = true;
  113.  
  114. while (run_client) {
  115. if (console_input.ready())
  116. handle_command(console_input.readLine());
  117.  
  118. while (socket_input.ready()) {
  119. String input_line = socket_input.readLine();
  120. System.out.println(input_line);
  121. }
  122. }
  123. }
  124.  
  125. // Disconnect from the MUSH
  126. public void shutdown() throws IOException {
  127. socket.close();
  128. connected = false;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement