// ======================================================================== // // Simple java client for socket-level programming. // // ======================================================================== import java.io.*; import java.net.*; public class Client { public final static int DEFAULT_PORT = 9999; // Exit if an exception occurs static public void Fail (Exception e, String msg) { System.err.println (msg + ":" + e); System.exit (1); } // Create a client socket to communicate over public static void main (String [] argv) { int ct, port; InputStream instrm; OutputStream outstrm; DataInputStream dinstrm; DataOutputStream doutstrm; char ch; String str; Socket client_sock; if (argv.length == 0) port = DEFAULT_PORT; else port = Integer.parseInt (argv[1]); try { // Attempt to connect to the server. client_sock = new Socket (argv[0], port); // The connection is made, so get the input and output // streams. instrm = client_sock.getInputStream (); dinstrm = new DataInputStream (instrm); outstrm = client_sock.getOutputStream (); doutstrm = new DataOutputStream (outstrm); // Get a line of text and send it off to the server System.out.println ("Enter a line of text"); while ((ch = (char) System.in.read ()) != '\n') doutstrm.writeChar (ch); doutstrm.writeChar ('\n'); // Get the response and dump it System.out.print ("The server says --> "); while ((ch = dinstrm.readChar ()) != '\n') System.out.print (ch); // Close down the socket and quit client_sock.close (); } catch (IOException e) { // this.Fail (e, "Exception on socket or IO"); } } }