import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; import java.lang.*; public class OBJClient extends Applet implements Runnable { public static final int PORT = 6789; public String[] objArray; Socket s; DataInputStream in; PrintStream out; TextArea outputarea; StreamListener listener; Thread myThread; String score; // Create a socket to communicate with a server on port 6789 of the // host that the applet's code is on. Create streams to use with // the socket. Then create a TextField for user input and a TextArea // for server output. Finally, create a thread to wait for and // display server output. public void init() { outputarea = new TextArea(); outputarea.setEditable(false); this.setLayout(new BorderLayout()); this.add("Center", outputarea); } public void run() { if (score.equals(null)) return; try { s = new Socket(this.getCodeBase().getHost(), PORT); in = new DataInputStream(s.getInputStream()); out = new PrintStream(s.getOutputStream()); out.println(score); } catch (IOException e) {this.showStatus("Exception: " + e);} listener = new StreamListener(in, outputarea); this.showStatus("Connected to " + s.getInetAddress().getHostName() + ":" + s.getPort()); } public void start() { if (myThread == null) { myThread = new Thread(this); myThread.start(); } } public void setScore(String aString) { score = aString; myThread = null; } } // Wait for output from the server on the specified stream, and display // it in the specified TextArea. class StreamListener extends Thread { DataInputStream in; TextArea output; public StreamListener(DataInputStream in, TextArea output) { this.in = in; this.output = output; this.start(); } public void run() { try { String line; while ((line = in.readLine()) != null) if (line.startsWith("OBJ> ")) output.appendText(line.substring(5, line.length()) + "\n"); else output.appendText(line + "\n"); } catch (IOException e) {output.setText(e.toString());} finally {output.appendText("Connection closed by server.\n\n");} } }