import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; import java.io.*; public class DogApplet extends Applet { public void init() { super.init(); setLayout(null); resize(544,409); setFont(new Font("Dialog", Font.PLAIN, 12)); answer = new Button("Answer Server"); answer.setBounds(173,195,106,22); add(answer); AnswerListener answerListener = new AnswerListener() ; answer.addActionListener(answerListener); reset = new Button("Reset"); reset.setBounds(406,371,60,23); add(reset); resetter = new ResetListener(); reset.addActionListener(resetter); label1 = new Label("From Server"); label1.setBounds(30,65,115,24); add(label1); label2 = new Label("Your Reply"); label2.setBounds(29,157,103,25); add(label2); label3 = new Label("Server's Judgement"); label3.setBounds(24,268,133,23); add(label3); label4 = new Label("Are you a dog? Answer the following question."); label4.setBounds(56,12,407,24); add(label4); fromServer = new TextField(); // fromServer.setEditable(false); fromServer.setBackground(Color.white); fromServer.setBounds(170,67,360,36); fromServer.setFont(new Font("Dialog", Font.PLAIN, 14)); add(fromServer); fromServer.setEnabled(false); judgementFromServer = new TextField(); // judgementFromServer.setEditable(false); judgementFromServer.setBackground(Color.white); judgementFromServer.setBounds(170,261,360,36); judgementFromServer.setFont(new Font("Dialog", Font.PLAIN, 14)); add(judgementFromServer); judgementFromServer.setEnabled(false); userReply = new java.awt.TextField(); userReply.setBounds(170,150,360,36); userReply.setFont(new Font("Dialog", Font.PLAIN, 14)); userReply.addActionListener(answerListener); add(userReply); port = Integer.parseInt(getParameter("port")); try { if(sock != null) sock.close(); home = getDocumentBase(); sock = new Socket(home.getHost(),port); // Get I/O streams fromServer the socket dis = new BufferedReader(new InputStreamReader( sock.getInputStream() )); dat = new PrintStream( sock.getOutputStream() ); judgementFromServer.setText(""); String questionFromServer = dis.readLine(); fromServer.setText(questionFromServer); } catch(IOException e) { fromServer.setText("Sorry, IO Error: " + e); } userReply.setText(""); userReply.requestFocus(); } private class AnswerListener implements ActionListener { public void actionPerformed(ActionEvent evt) { String user = userReply.getText(); dat.println(user); try { String judgement = dis.readLine(); fromServer.setText(""); judgementFromServer.setText(judgement); sock.close(); } catch(IOException e) { fromServer.setText("Sorry, IO Error: " + e); } sock = null; } } public void stop() { try { sock.close(); } catch(Exception e) { } } public void start() { resetter.actionPerformed(null); } private class ResetListener implements ActionListener { public void actionPerformed(ActionEvent evt) { try { if(sock == null) { sock = new Socket(home.getHost(),port); // Get I/O streams fromServer the socket dis = new BufferedReader(new InputStreamReader( sock.getInputStream() )); dat = new PrintStream( sock.getOutputStream() ); judgementFromServer.setText(""); userReply.setText(""); String questionFromServer = dis.readLine(); fromServer.setText(questionFromServer); userReply.requestFocus(); } } catch(IOException e) { fromServer.setText("Sorry, IO Error: " + e); } } } Button answer; Button reset; Label label1; Label label2; Label label3; Label label4; TextField fromServer; TextField judgementFromServer; TextField userReply; URL home; int port = 4444; Socket sock = null; BufferedReader dis; PrintStream dat; ResetListener resetter = null; }