Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- // Implement a simple MUSH client in one class, including a static main() that
- // instantiates the class
- public class MUSHClient {
- // The internet hostname of the MUSH to connect to
- private String hostname;
- // The port on the server where the MUSH is located
- private int port;
- // The IP address of the server; we look it up early on in the object's life
- private InetAddress ip_address;
- // A socket that will be connected to the remove host
- private Socket socket;
- // For buffered reading of data from the socket
- private BufferedReader socket_input;
- // For writing data to the socket
- private PrintWriter socket_output;
- // Remember whether we are connected or not
- private boolean connected;
- // This is how the client knows it is time to quit the main loop
- private boolean run_client;
- // A sample driver for the client, although it could easily be instantiated
- // from other code
- public static void main(String[] args) {
- String hostname;
- int port;
- BufferedReader in;
- MUSHClient client;
- client = null;
- in = new BufferedReader(new InputStreamReader(System.in));
- // First, read in the hostname and port to connect to
- try {
- System.out.print("Hostname: ");
- hostname = in.readLine();
- System.out.print("Port: ");
- port = Integer.parseInt(in.readLine());
- } catch (Exception e) {
- System.out.println("Exception: " + e);
- return;
- }
- // Second, instantiate the client, connect it to the MUSH, and run the
- // main loop
- try {
- client = new MUSHClient(hostname, port);
- client.connect();
- client.run();
- } catch (Exception e) {
- System.out.println("Exception: " + e);
- } finally {
- // Clean up by disconnecting from the MUSH
- try {
- if (client != null && client.connected())
- client.shutdown();
- } catch (Exception e) {
- System.out.println("Exception: " + e);
- }
- }
- }
- // Constructor; takes a hostname and port to connect to and performs the
- // DNS lookup
- public MUSHClient(String hostname, int port) throws UnknownHostException {
- this.hostname = hostname;
- this.port = port;
- ip_address = InetAddress.getByName(hostname);
- connected = false;
- }
- // Return whether the client is currently in a connected state
- public boolean connected() {
- return connected;
- }
- // Connect to the MUSH by creating a socket connection and the appropriate
- // reader and writer objects, then setting the connected attribute
- public void connect() throws IOException {
- socket = new Socket(ip_address, port);
- socket_input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- socket_output = new PrintWriter(socket.getOutputStream(), true);
- connected = true;
- }
- // Handle a command from the user, which currently should be either /quit or
- // a line of text to pass on to the MUSH
- public void handle_command(String command) {
- if (command.equals("/quit")) {
- run_client = false;
- return;
- }
- socket_output.println(command);
- }
- // Run the main loop until the user types /quit
- public void run() throws IOException {
- BufferedReader console_input = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Type /quit to quit.");
- run_client = true;
- while (run_client) {
- if (console_input.ready())
- handle_command(console_input.readLine());
- while (socket_input.ready()) {
- String input_line = socket_input.readLine();
- System.out.println(input_line);
- }
- }
- }
- // Disconnect from the MUSH
- public void shutdown() throws IOException {
- socket.close();
- connected = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement