import java.io.*; import java.net.*; /* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved. A simple client, implemented as a standalone program, not an applet. Slighly modified code taken from the excellent SUN online Java tutorial. */ public class JokeClient { public static void main(String[] args) { try { Socket JokeSocket = new Socket("kitten.shu.edu", 4444); PrintStream outStream = new PrintStream( JokeSocket.getOutputStream()); DataInputStream inStream = new DataInputStream( JokeSocket.getInputStream()); StringBuffer buf = new StringBuffer(50); int aChar; String fromServer; while ((fromServer = inStream.readLine()) != null) { System.out.println("Server: " + fromServer); if (fromServer.equals("Bye")) break; while ((aChar = System.in.read()) != '\n') { buf.append((char)aChar); } System.out.println("Client: " + buf); outStream.println(buf.toString()); outStream.flush(); buf.setLength(0); } outStream.close(); inStream.close(); JokeSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (Exception e) { System.err.println("Exception: " + e); } } }