import java.io.*; import java.net.*; /* *************************************************************** Bert G. Wachsmuth June 3, 1996 LineClient object Connects to a socket on a machine through tcpip and offers send and receive lines through that socket to a line server. This object offers the following public methods: public LineClient(String _Host, int _Port) - the constructor PRE: _Host is a string indicating the Internet address of a Host, _Port is the port number to use for a connection POST: Instantiates a LineClient public String Open() PRE: LineClient must be instantiated POST: Opens connection to port on host and sets up input and output streams. Blocks server for any other connection. Returns a string containing identifier sent by server. public String Close() PRE: LineClient is instantiated and connection has been opened. POST: Closes streams and socket and frees server for next connection. public String ReceiveLine() PRE: LineClient is instantiated and connection has been opened. POST: Returns response from server as String. public void SendLine(String s) or public void SendLine(int i) PRE: LineClient is instantiated and connection has been opened. POST: Sends a String s through the socket to the server. NOTE: The server *must* send one identifier line immediately on connect (returned in open() method). *************************************************************** */ public class LineClient { String Host; int Port; Socket theSocket; PrintStream outStream; DataInputStream inStream; /* *************************************************************** */ public LineClient(String _Host, int _Port) { Host = _Host; Port = _Port; } /* *************************************************************** */ public String Open() { try { theSocket = new Socket(Host, Port); outStream = new PrintStream(theSocket.getOutputStream()); inStream = new DataInputStream(theSocket.getInputStream()); return ReceiveLine(); } catch (UnknownHostException e) { System.err.println("Unknown host exception:" + e); return "ERROR: Unknown host."; } catch (Exception e) { System.err.println("Unknown exception: " + e); return "ERROR: Unknown exception."; } } /* *************************************************************** */ public String Close() { try { outStream.close(); inStream.close(); theSocket.close(); return "Connection closed."; } catch(Exception e) { System.err.println("Could not clean up. Exception: " + e); return "ERROR: connection broke before closing."; } } /* *************************************************************** */ public String ReceiveLine() { try { return inStream.readLine(); } catch(Exception e) { System.err.println("Unknown IO exception: " + e); Close(); return null; } } /* *************************************************************** */ public void SendLine(String s) { try { outStream.println(s); outStream.flush(); } catch(Exception e) { System.err.println("Unknown IO exception: " + e); Close(); } } /* *************************************************************** */ public void SendLine(int i) { try { outStream.println(i); outStream.flush(); } catch(Exception e) { System.err.println("Unknown IO exception: " + e); Close(); } } /* **************************************************** */ }