//Copyright (c) 1998, Arthur Gittleman //This example is provided WITHOUT ANY WARRANTY either expressed or implied. import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class Ex2_19 extends Frame implements ActionListener { private String reply = ""; private Socket server; private BufferedReader fromServer; private PrintWriter toServer; private BufferedReader input; private TextField text = new TextField(); private Font f = new Font("Serif", Font.BOLD, 24); public Ex2_19(String title, String host) { super(title); try { server = new Socket(host,5678); System.out.println("Connected to ReverseSever host " + server.getInetAddress()); fromServer = new BufferedReader (new InputStreamReader(server.getInputStream())); toServer = new PrintWriter(server.getOutputStream(),true); input = new BufferedReader(new InputStreamReader(System.in)); }catch(Exception e) { e.printStackTrace(); } add(text,"North"); text.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { try { fromServer.close(); toServer.close(); input.close(); server.close(); }catch(Exception e) { e.printStackTrace(); } System.exit(0); }}); setFont(f); } public void actionPerformed(ActionEvent evt) { toServer.println(text.getText()); try { reply = fromServer.readLine(); } catch (IOException e) { reply = "Error from server"; } repaint(); } public void paint(Graphics g) { g.drawString(reply,50, 150); } public static void main(String [] args) { Ex2_19 f = new Ex2_19("Reverse",args[0]); f.setSize(500,400); f.setVisible(true); } }