/* client.java - a simple stream client */ import java.io.*; import java.net.*; public class client { public static void main(String[] args) throws IOException { Socket sock = null; PrintWriter out = null; BufferedReader in = null; int port=0; String msg = "Hello from the client"; if (args.length != 2) { System.err.println("usage java client host port"); System.exit(0); } try { port = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.err.println("Second Arg was not a number"); System.exit(0); } try { sock = new Socket(args[0],port); out = new PrintWriter(sock.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( sock.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + args[0]); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: " + args[0]); System.exit(1); } try { out.println(msg); char buffer[] = new char[4096]; int n; n = in.read(buffer,0,4096); String s = new String(buffer,0,n); System.out.println("Read " + n + " chars from the socket: " + s); } catch(IOException e) { System.err.println("Error reading from the socket"); System.exit(0); } out.close(); in.close(); sock.close(); } }