import java.applet.Applet;
import java.awt.*;

import java.net.Socket;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.io.IOException;

public class CLUGApplet3 extends CLUGApplet1 {
  
  ObjectOutputStream oos;
  ObjectInputStream ois;
  
  public void init() {
    super.init();
    try {
      Socket s=null;
      if (isApplet)
        s = new Socket(getCodeBase().getHost(), 2513);
      else 
        s = new Socket("localhost", 2513);
      oos = new ObjectOutputStream(s.getOutputStream());
      ois = new ObjectInputStream(s.getInputStream());    
    } catch (IOException iox) {
      System.err.println(iox);
    };
  }
  
  public boolean action(Event e, Object o) {
    if (o.equals("=")) {
      int n1=0, n2=0;
      try {
        n1 = Integer.parseInt(faktor1.getText());
        n2 = Integer.parseInt(faktor2.getText());        
      } catch (NumberFormatException ex) {
        System.err.println(ex);
      }
      
      try {
        
        Job j = new Job(n1, n2);
        System.out.println("writing "+j);
        oos.writeObject(j);
        oos.flush();
        
        j = (Job) ois.readObject();
        produkt.setText(""+j.getResult());

      } catch (IOException ex) {
        System.err.println(ex);
      } catch (ClassNotFoundException ex) {
        System.err.println(ex);
      }
      return true;  
    }
    return super.action(e, o);
  }
  
  public static void main(String [] param) {
    Frame f = new ExtFrame("CLUG-Applet in a Frame (ObjectServer)");
    Applet a;
    isApplet = false;
    
    f.add(a = new CLUGApplet3());
    a.init();
    f.pack();
    f.show();
  }
}

