// Copyright 1997, MageLang Institute. import java.io.*; import java.net.UnknownHostException; import java.net.Socket; import java.net.InetAddress; public class Client { public static final int DEFAULT_PORT = 8002; protected DataInputStream in; protected PrintStream out; protected Socket client; public static void main(String[] args) { // first determine the port to use int port = DEFAULT_PORT; if (args.length > 1) { System.err.println("Usage: java Client [port]"); System.exit(0); } else if (args.length == 1) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Invalid port number" + port); System.exit(0); } } // create a new client try { Client c = new Client(InetAddress.getLocalHost().getHostAddress(), port); } catch (Exception e) { System.err.println(e); } } public Client(String host, int port) throws IOException, UnknownHostException { // connect to host at port System.out.println("test"); client = new Socket(host, port); System.out.println("got server"); // first build a tree TreeNode top = new TreeNode("top"); top.addChild(new TreeNode("left")); top.addChild(new TreeNode("right")); // print it out to see how it looks System.out.println("source tree: \n" + a.toString()); // now write the tree to the socket, then read it back in again try { ObjectOutput out = new ObjectOutputStream(client.getOutputStream()); out.writeObject(top); out.flush(); ObjectInputStream in = new ObjectInputStream(client.getInputStream()); TreeNode n = (TreeNode)in.readObject(); System.out.println("read tree: \n" + n.toString()); } catch (Exception e) { System.err.println("exception: " + e); } } }