import java.net.*; import java.io.*; import java.awt.event.*; import java.awt.*; public class jutukas implements ActionListener{ TextArea ta=new TextArea(25, 80); TextField tf=new TextField(""); Socket sc; PrintWriter valja; jutukas() throws Exception{ sc = new Socket("ws7.cs1.tpu.ee", 3001 ); valja = new PrintWriter(sc.getOutputStream(), true); Frame f=new Frame("Raam"); ta.setEditable(false); new Sonumiloim(ta, sc); f.addWindowListener (new WindowAdapter(){ public void windowClosing(WindowEvent e){ Sonumiloim.close(); System.exit(0); }}); f.add (tf, BorderLayout.SOUTH); f.add(ta, BorderLayout.CENTER); f.setSize(600, 500); tf.addActionListener(this); tf.setBackground(Color.red); ta.setBackground(Color.green); f.setVisible(true); } public static void main(String args[])throws Exception{ new jutukas(); } public void actionPerformed(ActionEvent e){ try{ valja.println(tf.getText()); tf.setText(""); tf.requestFocus(); }catch(Exception viga){viga.printStackTrace();} } } class Sonumiloim extends Thread{ TextArea ta; Socket sc; public static PrintWriter out = null; File fail; int i = 0; String tmp = new String("jututuba.l0"); public Sonumiloim(TextArea ta1, Socket sc1){ ta=ta1; sc=sc1; start(); } public void run(){ try{ fail = new File("jututuba.log"); if (fail.exists()) { do { fail = new File(tmp.substring(0, 12- new Integer(i).toString().length())+new Integer(i).toString()); i++; } while (fail.exists()); } out = new PrintWriter(new FileWriter(fail)); BufferedReader sisse=new BufferedReader( new InputStreamReader(sc.getInputStream()) ); String rida=sisse.readLine(); while(rida!=null){ ta.append(rida+"\n"); out.println(rida); rida=sisse.readLine(); } }catch(Exception e){} } public static void close() { if (out != null) { out.close(); } else { System.out.println("Väljundfaili ei suudetud kirjutada"); } } }