/** Start talking to the server */ public void start() { String codehost = getCodeBase().getHost(); if (socket == null) { try { // Open the socket to the server socket = new Socket(codehost, port); // Create output stream out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); // Create input stream and start background // thread to read data from the server in = new ObjectInputStream(socket.getInputStream()); new Thread(this).start(); } catch (Exception e) { // Exceptions here are unexpected, but we can't // really do anything (so just write it to stdout // in case someone cares and then ignore it) System.out.println("Exception! " + e.toString()); e.printStackTrace(); setErrorStatus(STATUS_NOCONNECT); socket = null; } } else { // Already started } if (socket != null) { // Make sure the right buttons are enabled start_button.setEnabled(false); stop_button.setEnabled(true); setStatus(STATUS_ACTIVE); } }
/** Stop talking to the server */ public void stop() { if (socket != null) { // Close all the streams and socket if (out != null) { try { out.close(); } catch (IOException ioe) { } out = null; } if (in != null) { try { in.close(); } catch (IOException ioe) { } in = null; } if (socket != null) { try { socket.close(); } catch (IOException ioe) { } socket = null; } } else { // Already stopped } // Make sure the right buttons are enabled start_button.setEnabled(true); stop_button.setEnabled(false); setStatus(STATUS_STOPPED); }