|
|
@@ -3,7 +3,6 @@ |
|
|
|
* To change this template file, choose Tools | Templates |
|
|
|
* and open the template in the editor. |
|
|
|
*/ |
|
|
|
|
|
|
|
package model; |
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
@@ -29,103 +28,127 @@ public class Transmitter extends Observable implements Runnable |
|
|
|
private static final Logger lg = OhmLogger.getLogger(); |
|
|
|
private static final int PORT = 35000; |
|
|
|
private static final String IP_ADRESSE = "127.0.0.1"; |
|
|
|
|
|
|
|
|
|
|
|
int modus; |
|
|
|
private String nachricht; |
|
|
|
String message; |
|
|
|
OutputStream oStream; |
|
|
|
InputStream iStream; |
|
|
|
InputStreamReader isr; |
|
|
|
OutputStreamWriter osr; |
|
|
|
BufferedReader in; |
|
|
|
PrintWriter out; |
|
|
|
Socket s; |
|
|
|
|
|
|
|
public Transmitter(int modus) |
|
|
|
{ |
|
|
|
|
|
|
|
this.modus = modus; |
|
|
|
nachricht = ""; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void run() |
|
|
|
{ |
|
|
|
if(modus == 0) |
|
|
|
if (modus == 0) |
|
|
|
{ |
|
|
|
ServerSocket sSocket; |
|
|
|
try |
|
|
|
{ |
|
|
|
server(); |
|
|
|
sSocket = new ServerSocket(PORT); |
|
|
|
lg.info("Server: Warte auf Verbindung ..."); |
|
|
|
s = sSocket.accept(); // Achtung: blockiert! |
|
|
|
lg.info("Server: Verbindung akzeptiert"); |
|
|
|
} |
|
|
|
catch (IOException ex) |
|
|
|
{ |
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
else if(modus == 1) |
|
|
|
else if (modus == 1) |
|
|
|
{ |
|
|
|
lg.info("Client: verbinde ..."); |
|
|
|
try |
|
|
|
{ |
|
|
|
client(); |
|
|
|
s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert! |
|
|
|
lg.info("Client: Verbindung hergestellt"); |
|
|
|
} |
|
|
|
catch (IOException ex) |
|
|
|
{ |
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void client() throws IOException |
|
|
|
{ |
|
|
|
lg.info("Client: verbinde ..."); |
|
|
|
Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert! |
|
|
|
lg.info("Client: Verbindung hergestellt"); |
|
|
|
InputStream iStream = s.getInputStream(); |
|
|
|
OutputStream oStream = s.getOutputStream(); |
|
|
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); |
|
|
|
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); |
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(isr); |
|
|
|
//BufferedWriter out = new BufferedWriter(osr); |
|
|
|
PrintWriter out = new PrintWriter(osr); |
|
|
|
|
|
|
|
lg.info("Client: Stream initialisiert"); |
|
|
|
|
|
|
|
out.println("Hallo Du Server Du - ich bin der client"); |
|
|
|
out.flush(); // wirklich absenden!! |
|
|
|
|
|
|
|
lg.info("Client: Nachricht versendet"); |
|
|
|
|
|
|
|
String quittung = in.readLine(); // Achtung blockiert |
|
|
|
lg.info("Client: Quittung empfangen"); |
|
|
|
|
|
|
|
System.out.println("Client: Quittung EMPFANGEN - " + quittung); |
|
|
|
try |
|
|
|
{ |
|
|
|
iStream = s.getInputStream(); |
|
|
|
oStream = s.getOutputStream(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out.close(); |
|
|
|
in.close(); |
|
|
|
} |
|
|
|
isr = new InputStreamReader(iStream, "UTF-8"); |
|
|
|
osr = new OutputStreamWriter(oStream, "UTF-8"); |
|
|
|
|
|
|
|
public void server() throws IOException |
|
|
|
{ |
|
|
|
ServerSocket sSocket = new ServerSocket(PORT); |
|
|
|
lg.info("Server: Warte auf Verbindung ..."); |
|
|
|
Socket s = sSocket.accept(); // Achtung: blockiert! |
|
|
|
lg.info("Server: Verbindung akzeptiert"); |
|
|
|
InputStream iStream = s.getInputStream(); |
|
|
|
OutputStream oStream = s.getOutputStream(); |
|
|
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); |
|
|
|
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); |
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(isr); |
|
|
|
//BufferedWriter out = new BufferedWriter(osr); |
|
|
|
PrintWriter out = new PrintWriter(osr); |
|
|
|
|
|
|
|
lg.info("Server: Stream initialisiert"); |
|
|
|
lg.info("Server: warte auf Nachricht ..."); |
|
|
|
|
|
|
|
String nachricht = in.readLine(); // Achtung blockiert |
|
|
|
lg.info("Server: Nachricht empfangen"); |
|
|
|
in = new BufferedReader(isr); |
|
|
|
//BufferedWriter out = new BufferedWriter(osr); |
|
|
|
out = new PrintWriter(osr); |
|
|
|
} |
|
|
|
catch (IOException ex) |
|
|
|
{ |
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
if (modus == 0) |
|
|
|
{ |
|
|
|
lg.info("Server: Stream initialisiert"); |
|
|
|
} |
|
|
|
else if (modus == 1) |
|
|
|
{ |
|
|
|
lg.info("Client: Stream initialisiert"); |
|
|
|
} |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
nachricht = in.readLine(); // Achtung blockiert |
|
|
|
} |
|
|
|
catch (IOException ex) |
|
|
|
{ |
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
|
|
|
|
System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht); |
|
|
|
setChanged(); |
|
|
|
notifyObservers(); |
|
|
|
|
|
|
|
lg.info("Server: Nachricht empfangen"); |
|
|
|
out.flush(); // wirklich absenden!! |
|
|
|
|
|
|
|
if (nachricht.equals("beenden")) |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
out.println("Server -> ich habe die Nachricht erhalten"); |
|
|
|
lg.info("Server: Quittung versendet"); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
in.close(); |
|
|
|
out.close(); |
|
|
|
|
|
|
|
} |
|
|
|
catch (IOException ex) |
|
|
|
{ |
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void send(String input) |
|
|
|
{ |
|
|
|
out.println(input); |
|
|
|
out.flush(); // wirklich absenden!! |
|
|
|
|
|
|
|
out.close(); |
|
|
|
in.close(); |
|
|
|
} |
|
|
|
/** |
|
|
|
* @return the nachricht |
|
|
|
*/ |
|
|
|
public String getNachricht() |
|
|
|
{ |
|
|
|
System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht); |
|
|
|
return nachricht; |
|
|
|
} |
|
|
|
|
|
|
|
} |