// This example from the book Java in a Nutshell by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Assc. // You may study, modify, this example for any purpose. // This example is WITHOUT WARRANTY either expressed or implied. // Modified by Roger Webster, Ph.D. import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class appletclient extends Applet { public static final int PORT = 7777; Socket s; DataInputStream in; PrintStream out; TextField inputfield; TextArea outputarea; StreamListener listener; // Create a socket to communicate with a server on port 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() { try { inputfield = new TextField(20); outputarea = new TextArea(5,20); outputarea.setEditable(false); add(inputfield); add(outputarea); this.show(); s = new Socket(this.getCodeBase().getHost(), PORT); in = new DataInputStream(s.getInputStream()); out = new PrintStream(s.getOutputStream()); System.out.println("after socket creation in applet!!"); listener = new StreamListener(in, outputarea); System.out.println("Connected to " + s.getInetAddress().getHostName() + ":" + s.getPort()); } catch (IOException e) { System.out.println(e.toString()); PopUpWindow window = new PopUpWindow("Socket Error", e.toString()); } } // When the user types a line, send it to the server. public boolean action(Event e, Object what) { if (e.target == inputfield) { out.println((String)e.arg); inputfield.setText(""); return true; } return false; } } class PopUpWindow extends Frame { public Panel buttonPanel; public Button close; public PopUpWindow(String title, String mesg){ //Instantiate variables super(title); Label return_message = new Label(mesg); buttonPanel = new Panel(); close = new Button("OK"); //Add button to panel and panel to window setLayout(new FlowLayout()); buttonPanel.setLayout(new GridLayout(2,1)); buttonPanel.add(return_message); buttonPanel.add(close); add(buttonPanel); //Size window to preferred size this.reshape(100, 100, 300, 150); move(150,300); close.requestFocus(); show(); }//end PopUpWindow(String, String) // uses the jdk1.0.2 event handler... public boolean action(Event e, Object arg){ if(e.target instanceof Button){ if(e.target == close){ if(this.isShowing()) { dispose(); } } } return true; }//end action(Event, Object) }//end class definition // 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() { String line; try { for(;;) { line = in.readLine(); if (line == null) break; output.appendText(line); } } catch (IOException e) { PopUpWindow window = new PopUpWindow("Socket Error", e.toString()); output.setText(e.toString()); } finally {output.setText("end of loop Connection closed by server.");} } }