Java Sockets


Das Schema des Nachrichtenaustauschs ist in Abbildung 2 dargestellt.

 

Abbildung 2: Schema des Nachrichtenaustauschs

Sockets

Spezifikation von ServerSocket:

 public ServerSocket(int port) throws IOException 
 public Socket accept() throws IOException

Spezifikationen von Socket

 public Socket(String host, int port)
        throws UnknownHostException, IOException
 public InputStream getInputStream() throws IOException
 public OutputStream getOutputStream() throws IOException

 

UML Socket

Abbildung E: UML Socket

Beispiel: HelloWorldClientByte.java HelloWorldServByte.java

Zuordnung von passenden Datenströmen:

Spezifikation der benötigten Konstruktoren und Methoden

 public ObjectOutputStream(OutputStream out) throws IOException 
 public void flush() throws IOException
 public ObjectInputStream(InputStream in)
        throws IOException, StreamCorruptedException

 

UML Serialization

Abbildung F: UML Serialization

Datenübertragung mit Send-Operation (send) und Empfangs-Operation (recieve).

Spezifikation aus ObjectOutputStream

 public final void writeObject(Object obj)
        throws IOException

Spezifikation aus ObjectInputStream

 public final Object readObject()
        throws OptionalDataException, 
               ClassNotFoundException, 
               IOException

SocketChannel

Der Konstruktor nimmt einen Socket als Eingabe und setzt die Objekt-Ströme aus den entsprechenden Strömen auf.

Die Methode close() dient zum schließen des Kanals.

send()- und receive()-Methoden dienen der Datenübertragung

 import java.io.*;
 import java.net.*;
   
 public class SocketChannel {

     private ObjectInputStream in;
     private ObjectOutputStream out;
   
     private Socket soc;
   
     public SocketChannel(Socket s) throws IOException {
       soc = s;
       if (checkOrder(s)) {
         in = new ObjectInputStream(s.getInputStream());
         out = new ObjectOutputStream(
                   s.getOutputStream());
       }
       else {
         out = new ObjectOutputStream(
                   s.getOutputStream());
         in = new ObjectInputStream(s.getInputStream());
       }
     }

     public void close() {
       if (in != null) {
          try { in.close(); } catch (IOException e) { }
       }
       if (out != null) {
          try { out.close(); } catch (IOException e) { }
       }
       if (soc != null) {
          try { soc.close(); } catch (IOException e) { }
       }
     }
   
     private boolean checkOrder(Socket s) 
                     throws IOException {
       int p1 = s.getLocalPort();
       int p2 = s.getPort();
       if (p1 < p2) return true;
       else if (p1 > p2) return false;
   
       int a1 = s.getLocalAddress().hashCode();
       int a2 = s.getInetAddress().hashCode();
       if (a1 < a2) return true;
       else if (a1 > a2) return false;
   
       throw new IOException(); // this shouldn't happen
     }
 }

Die Sende- und Empfangs-Methoden sind wie folgt.

 public void send(Object v) throws IOException {
    out.writeObject(v);
 }
Im Fehlerfall wird eine IOException ausgelöst, die dann vom Aufrufer behandelt werden muß.
 public Object receive() 
               throws IOException, 
                      ClassNotFoundException {
    return in.readObject();
 }
Neben einer IOException kann auch noch eine ClassNotFoundException ausgelöst werden, falls das Objekt zu einer dem Empfänger unbekannten Klasse gehört.

Beispiel HelloWorld mit SocketChannel

 

 

ExCon

Abbildung A1: HelloWorld

Implementierung: HelloWorldClient.java HelloWorldServ.java SocketChannel.java


© Universität Mannheim, Rechenzentrum, 2000-2007.

Heinz Kredel

Last modified: Sun Jan 28 13:25:50 CET 2007