import java.applet.Applet;
import java.awt.*;

import java.net.Socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.io.IOException;

public class CLUGApplet2 extends CLUGApplet1 {
  
  DataOutputStream dos;
  DataInputStream dis;
  
  public void init() {
    super.init();
    try {
      Socket s=null;
      if (isApplet)
        s = new Socket(getCodeBase().getHost(), 2512);
      else 
        s = new Socket("localhost", 2512);
      dos = new DataOutputStream(s.getOutputStream());
      dis = new DataInputStream(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 {
        dos.writeInt(n1);
        System.out.println("writing "+n1);
        dos.writeInt(n2);
        System.out.println("writing "+n2);
        dos.flush();
        produkt.setText(dis.readUTF());
      } catch (IOException 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 (Stream Server)");
    Applet a;
    isApplet = false;
    
    f.add(a = new CLUGApplet2());
    a.init();
    f.pack();
    f.show();
  }
}

