import java.io.*; import java.net.*; import java.applet.Applet; import java.awt.*; /* *************************************************************** Bert G. Wachsmuth June 3, 1996 A client program for Knock Knock jokes, implemented as an applet. There are three classes for this program: JokeClient: contains client part of the protocol. The commands the client can handle are 'get' and 'bye'. JokePlayer: if a joke was received, this object plays the joke interactively inside the JokeClient applet. LineClient: Sets up the connections to the server. Provides four public methods: Open: establishes connection, blocks server Close: closes connection, frees server SendLine: sends string or integer to server ReceiveLine: receives string from server This object can be used for any line-based client-server application. *************************************************************** */ public class JokeClient extends Applet { private static String theHost = "kitten.shu.edu"; private static int thePort = 4444; private String ButtonLabel = "Get new Joke"; private Button JokeControl = new Button(ButtonLabel); private List Jokes = new List(); private LineClient theClient = new LineClient(theHost, thePort); private JokePlayer Player; /* *************************************************************** */ public void init() { setLayout(new BorderLayout()); add("Center",Jokes); add("South",JokeControl); validate(); resize(300,200); } /* *************************************************************** */ private void getJoke() { showStatus(theClient.Open()); theClient.SendLine("get"); String Clue = theClient.ReceiveLine(); String Punch = theClient.ReceiveLine(); theClient.SendLine("bye"); showStatus(theClient.Close()); Player = new JokePlayer(JokeControl,Jokes, Clue, Punch, ButtonLabel); } /* *************************************************************** */ public boolean handleEvent(Event e) { if (e.target instanceof Button) { if (e.arg.equals(ButtonLabel)) { Jokes.delItems(0,Jokes.countItems()-1); Jokes.clear(); getJoke(); return true; } else return Player.handleButton(e.arg); } else return super.handleEvent(e); } /* *************************************************************** */ }