/********************************************************** * Filename : gameclient.java * Original Author : Andrew Miller * Modified by : Dr. Roger Webster * (fixed up some exception handling) * This class interfaces the network classes of the * GameServer. Most members are quite self explanatory **********************************************************/ import java.net.*; import java.io.*; // class declaration of gameclient class gameclient { private boolean connect_state = false; private Socket to_server; private DataInputStream in = null; private PrintStream out = null; public boolean is_connected() { return connect_state; } public boolean running() { if (!connect_state || in == null || out == null) return true; else return false; } // return whether or not the string is a names message from the server public boolean is_nameslist(String text) { if (text.startsWith("!!names!!")) return true; else return false; } // return whether or not the string is a directive message from the server public boolean is_directive(String text) { if (text.startsWith("!!directive!!")) return true; else return false; } // return whether or not there is an input stream available public boolean input_stream_available() { try { if (in.available() > 0) return true; } catch (Exception e) {} return false; } // return whether or not any of the IO streams are not functioning public boolean stream_down() { if (out == null || in == null) return true; else return false; } public void disconnect() { if (out != null && in != null){ out.println("!!quit!!"); out.flush(); try{ out.close(); in.close(); if (to_server != null) to_server.close(); }catch(Exception e){} connect_state = false; } } // sends string through socket to server public void send_stream(String text) { out.println(text); out.flush(); } // receives string through the socket and returns it public String get_input() { try { return (in.readLine() + '\n'); } catch (IOException e) {} return null; } public boolean connect(String host, int port) { System.out.println("Trying to connect to host is " + host); System.out.println("On port " + port); try { try { to_server = new Socket(host,port); } catch(Exception e) { System.out.println("new Socket error" + e.toString()); return false; } in = new DataInputStream(new BufferedInputStream(to_server.getInputStream())); out = new PrintStream (new BufferedOutputStream(to_server.getOutputStream(),1024), false); } catch(Exception e) { System.out.println("Socket error" + e.toString()); return false; } connect_state = true; return true; } // cleanup routine that shuts things down public void cleanup() { if (out != null) { out.println("!!quit!!"); out.flush(); } try { if (out != null) out.close(); if (in != null) in.close(); if (to_server != null) to_server.close(); }catch(IOException e){} connect_state = false; // no longer connected } } // end class