import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; public class Client extends JFrame implements Runnable { private Thread thread = new Thread(this); private boolean running = true; private Socket s = null; private ObjectInputStream in = null; private ObjectOutputStream out = null; private static String host = "atlas.dsv.su.se"; private static int port = 4848; private Sound sound = new Sound(); private JButton recordButton = new JButton("Record"); private JButton stopButton = new JButton("Stop&Send"); public static void main(String args[]) { if(args.length != 0) { host = args[0]; if(args.length != 1) { port = Integer.parseInt(args[1]); } } new Client(); } public Client() { setDefaultCloseOperation(EXIT_ON_CLOSE); recordButton.setEnabled(true); stopButton.setEnabled(false); recordButton.addActionListener(new LRecord()); stopButton.addActionListener(new LStop()); getContentPane().setLayout(new GridLayout()); getContentPane().add(recordButton); getContentPane().add(stopButton); pack(); setVisible(true); try { s = new Socket(host, port); setTitle("CONNECTED TO: " + host + " - ON PORT: " + port); out = new ObjectOutputStream(s.getOutputStream()); } catch (IOException ioe) { System.out.println("COULD NOT CONNECT: "+ ioe); System.exit(1); } thread.start(); } public void run() { while(running) { try { // Obs! Detta maste vara har!!! in = new ObjectInputStream(s.getInputStream()); Storage storage = (Storage)in.readObject(); System.out.println("Sound received, size: " + storage.getData().length); sound.load(storage); sound.play(); } catch(Exception e) { //System.out.println("ERROR in RUN: "+ e); } } } class LRecord implements ActionListener { public void actionPerformed(ActionEvent e) { recordButton.setEnabled(false); stopButton.setEnabled(true); sound.record(); } } class LStop implements ActionListener { public void actionPerformed(ActionEvent e) { recordButton.setEnabled(true); stopButton.setEnabled(false); sound.stop(); while(sound.recorderRunning) { try {Thread.sleep(100);} catch(Exception ee){} } Storage storage = sound.get(); try { System.out.println("Sending sound, size: " + storage.getData().length); out.writeObject(storage); System.out.println("Sound sent!"); } catch(IOException ioe) { System.err.println("I/O problems: " + e); } } } }