import java.net.*; import java.io.*;
class ZaehlerServer{ int zaehler=0; public void incr(){ zaehler++; // return zaehler; } public void decr(){ zaehler--; // return zaehler; } public void betrieb(){ try{ ServerSocket serverso= new ServerSocket(1234); while (true) { String kommando; Socket so=serverso.accept(); BufferedReader vomClient = new BufferedReader( new InputStreamReader(so.getInputStream())); BufferedWriter zumClient= new BufferedWriter( new OutputStreamWriter(so.getOutputStream())); kommando= vomClient.readLine(); if (kommando.equals("incr")) { incr(); } // end of if else if (kommando.equals("decr")){ decr(); } // end of if-else zumClient.write(""+zaehler); zumClient.flush(); so.close(); } // end of while } catch (IOException e){ } } public static void main(String[] args){ ZaehlerServer zs = new ZaehlerServer(); zs.betrieb(); } }
import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*;
/** * * Beschreibung * * @version 1.0 vom 05.07.2015 * @author */
public class Client extends Frame { // Anfang Attribute private Button button1 = new Button(); private Button button2 = new Button(); private TextField textField1 = new TextField(); // Ende Attribute public Client(String title) { // Frame-Initialisierung super(title); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); } }); int frameWidth = 386; int frameHeight = 171; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y); setResizable(false); Panel cp = new Panel(null); add(cp); // Anfang Komponenten button1.setBounds(48, 40, 57, 41); button1.setLabel("<"); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { button1_ActionPerformed(evt); } }); cp.add(button1); button2.setBounds(248, 40, 57, 41); button2.setLabel(">"); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { button2_ActionPerformed(evt); } }); cp.add(button2); textField1.setBounds(128, 40, 89, 41); cp.add(textField1); // Ende Komponenten setVisible(true); kommando("get"); } // end of public Client // Anfang Methoden public void button1_ActionPerformed(ActionEvent evt) { kommando("decr"); } // end of button1_ActionPerformed public void button2_ActionPerformed(ActionEvent evt) { kommando("incr"); } // end of button2_ActionPerformed public void kommando(String befehl){ try{ String zaehler; Socket so= new Socket("localhost",1234); PrintWriter zumServer = new PrintWriter( so.getOutputStream(),true); BufferedReader vomServer = new BufferedReader( new InputStreamReader(so.getInputStream())); zumServer.println(befehl); zaehler= vomServer.readLine(); textField1.setText(zaehler); so.close(); } catch (IOException e){ textField1.setText("IO-Problem"); } } // Ende Methoden public static void main(String[] args) { new Client("Client"); } // end of main } // end of class Client
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*;
public class browser extends Applet { TextField tf; TextArea ta; public void init() { Button b1=new Button("Adresse"); add(b1);
tf=new TextField( "http://mylinux.rmz.uni-lueneburg.de/index.html"); tf.setColumns(40); add(tf); ta=new TextArea("Begin",20,60); ta.setText("Zeile 1"); ta.append("\nZeile 2"); ta.append("\nZeile 3"); add(ta);
b1.addMouseListener( new MouseAdapter(){ public void mouseClicked(MouseEvent e){ String eingabe; eingabe=tf.getText(); lesen(eingabe); } } ); }
public void lesen(String args) { try{ URL url=new URL(args); if (url.getProtocol().equals("http")){ ta.append("http"); Socket so=new Socket(url.getHost(),80); ta.append("Socket"); BufferedReader vomServer=new BufferedReader( new InputStreamReader(so.getInputStream())); PrintWriter zumServer=new PrintWriter( so.getOutputStream(),true); zumServer.println("GET "+url.getFile()+" HTTP/1.1\r\nHost: " +url.getHost()+"\r\n"); ta.append("GET"); // Lesen und Ausgabe der WWW-Seite int c= vomServer.read(); while(c!= -1){ ta.append(""+(char)c); c=vomServer.read(); } so.close(); } } catch(Exception e){ta.append("fehler"+e);}
} }
/* public class WWWbrowser { public static void main(String[] args) throws IOException { URL url=new URL(args[0]); if (url.getProtocol().equals("http")){ Socket so=new Socket(url.getHost(),80); BufferedReader vomServer=new BufferedReader( new InputStreamReader(so.getInputStream())); PrintWriter zumServer=new PrintWriter( so.getOutputStream(),true); zumServer.println("GET "+url.getFile()+" HTTP/1.0\r\n\r\n"); // Lesen und Ausgabe der WWW-Seite int c= vomServer.read(); while(c!= -1){ System.out.print(""+(char)c); c=vomServer.read();
} so.close(); } } } */
class Tier {}
class Fisch extends Tier {}
class Vogel extends Tier {}
class Huhn extends Vogel {}
class Spatz extends Vogel {}
class Karpfen extends Fisch {}
class Super {
public void m(Tier t1, Tier t2)
{ System.out.println("1"); }
public void m(Tier t, Fisch f)
{ System.out.println("2"); }
public void m(Fisch f, Tier t)
{ System.out.println("5"); }
}
class Sub extends Super {
public void m(Tier t1, Fisch t2)
{ System.out.println("3"); }
public void m(Vogel v, Fisch f)
{ System.out.println("4"); }
}
public class TierTest {
public static void main(String[] args) {
Tier t1 = new Tier();
Tier t2 = new Vogel();
Fisch f = new Karpfen();
Vogel v1 = new Vogel();
Vogel v2 = new Spatz();
Huhn h = new Huhn();
Karpfen k = new Karpfen();
Super sup1 = new Super();
Super sup2 = new Sub();
sup1.m(h, v2); // Aufruf #1
sup2.m(v1, k); // Aufruf #2
sup1.m(t1, t2); // Aufruf #3
sup1.m(v1, k); // Aufruf #4
sup1.m(v2, f); // Aufruf #5
sup2.m(v1, f); // Aufruf #6
}
}
Hierarchie:
Referenz |
deklarativer Typ |
dynamischer Typ |
sup1 |
Super |
Super |
sup2 |
Super |
Sub |
t1 |
Tier |
Tier |
t2 |
Tier |
Vogel |
f |
Fisch |
Karpfen |
v1 |
Vogel |
Vogel |
v2 |
Vogel |
Spatz |
h |
Huhn |
Huhn |
k |
Karpfen |
Karpfen |
sup1.m(h, v2); // Aufruf #1
Super.m(Huhn, Vogel); // Aufruf #1
System.out.println("1");
sup2.m(v1, k); // Aufruf #2
Super.m(Vogel, Karpfen); // Aufruf #2 (Sub)
System.out.println("3");
sup1.m(t1, t2); // Aufruf #3
Super.m(Tier, Tier); // Aufruf #3
System.out.println("1");
sup1.m(v1, k); // Aufruf #4
Super.m(Vogel, Karpfen); // Aufruf #4
System.out.println("2");
sup1.m(v2, f); // Aufruf #5
Super.m(Vogel, Fisch); // Aufruf #5
System.out.println("2");
sup2.m(v1, f); // Aufruf #6
Super.m(Vogel, Fisch); // Aufruf #6 (Sub)
System.out.println("3");