|
|
@@ -23,45 +23,58 @@ import java.util.logging.*; |
|
|
|
public class Server |
|
|
|
{ |
|
|
|
private static Logger lg = Logger.getLogger("netz"); |
|
|
|
private Socket socket; |
|
|
|
private BufferedReader reader; |
|
|
|
private PrintWriter writer; |
|
|
|
private static final int PORT = 35000; //lt. iana port > 2¹⁵ |
|
|
|
private static int timeout = 10000; |
|
|
|
|
|
|
|
|
|
|
|
public void connect() throws IOException |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
ServerSocket sSocket = new ServerSocket(PORT); |
|
|
|
sSocket.setSoTimeout(timeout); |
|
|
|
lg.info("Server: warte auf Verbindung"); |
|
|
|
socket = sSocket.accept(); |
|
|
|
lg.info("Server: Verbindung akzeptiert"); |
|
|
|
|
|
|
|
lg.info("Server: initialisiere reader und writer"); |
|
|
|
InputStream is = socket.getInputStream(); |
|
|
|
OutputStream os = socket.getOutputStream(); |
|
|
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); |
|
|
|
OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8"); |
|
|
|
|
|
|
|
reader = new BufferedReader(isr); |
|
|
|
writer = new PrintWriter(osr); |
|
|
|
lg.info("Server: Initialisierung abgeschlossen"); |
|
|
|
|
|
|
|
lg.info("Server: warte auf Nachricht"); |
|
|
|
|
|
|
|
} |
|
|
|
catch ( java.io.InterruptedIOException e ) |
|
|
|
{ |
|
|
|
lg.warning("Timeout"+"("+timeout/1000+"s)"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
public 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 is = s.getInputStream(); |
|
|
|
OutputStream os = s.getOutputStream(); |
|
|
|
|
|
|
|
//Umwandlung der Byteströme |
|
|
|
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); |
|
|
|
OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8"); |
|
|
|
|
|
|
|
//Puffer |
|
|
|
BufferedReader in = new BufferedReader(isr); |
|
|
|
//BufferedWriter out = new BufferedWriter(osr); |
|
|
|
//besser |
|
|
|
PrintWriter out = new PrintWriter(osr); |
|
|
|
|
|
|
|
lg.info("Server: Streams erfolgreich initialisiert"); |
|
|
|
lg.info("Server: warte auf Textnachricht"); |
|
|
|
|
|
|
|
String nachricht = in.readLine(); // ACHTUNG blockiert |
|
|
|
lg.info("Server: Nachricht erhalten"); |
|
|
|
System.out.println("Server: NACHRICHT = " + nachricht); |
|
|
|
|
|
|
|
// ACHTUNG: blockiert NICHT!!!! |
|
|
|
out.println("Server an Client: Nachricht erhalten"); |
|
|
|
out.flush(); // wichtig |
|
|
|
connect(); |
|
|
|
|
|
|
|
lg.info("Server: fertig"); |
|
|
|
in.close(); |
|
|
|
out.close(); |
|
|
|
s.close(); |
|
|
|
// String nachricht = in.readLine(); // ACHTUNG blockiert |
|
|
|
// lg.info("Server: Nachricht erhalten"); |
|
|
|
// System.out.println("Server: NACHRICHT = " + nachricht); |
|
|
|
// |
|
|
|
// // ACHTUNG: blockiert NICHT!!!! |
|
|
|
// out.println("Server an Client: Nachricht erhalten"); |
|
|
|
// out.flush(); // wichtig |
|
|
|
// |
|
|
|
// lg.info("Server: fertig"); |
|
|
|
// in.close(); |
|
|
|
// out.close(); |
|
|
|
// s.close(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |