Skip to content
Snippets Groups Projects
Commit 76e58e86 authored by mateis00's avatar mateis00
Browse files

Switch implemented

parent 2ac076da
No related branches found
No related tags found
No related merge requests found
......@@ -17,9 +17,9 @@ public class Client {
// Eingaben müssen aufgenommen werden:
BufferedReader info = new BufferedReader(new InputStreamReader(System.in)); //Eingabe von IP und Port, später Methodenaufrufe
System.out.println("IP-Adresse?");
System.out.print("IP-Adresse: ");
ip = info.readLine();
System.out.println("Port?");
System.out.print("Port: ");
String portFragezeichen = info.readLine(); // Port ist vorerst String
......@@ -65,25 +65,37 @@ public class Client {
System.out.println("Verbindung hergestellt, erwarte Auftrag:");
BufferedReader info2 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("command: ");
String eingabe = info2.readLine();
String eingabe = "";
while(true)
{
while(eingabe == "")
{
eingabe = info2.readLine();
}
if(eingabe.equalsIgnoreCase("close")){
if(eingabe.equalsIgnoreCase("close"))
{
zumServer.write("PING\n");
zumServer.flush();
break;
}
else if(eingabe.equalsIgnoreCase("ping")){
eingabe ="";
ping();
}
else if(eingabe.substring(0,4).equalsIgnoreCase("echo")){
echo(eingabe);
eingabe ="";
}
else if(eingabe.equalsIgnoreCase("current time")){
time();
eingabe ="";
}
else if(eingabe.equalsIgnoreCase("current date")){
date();
eingabe ="";
}
}
// "close" -> While schleife endet, constructor endet, client ist beendet
......@@ -95,6 +107,7 @@ public class Client {
zumServer.write("PING\n");
zumServer.flush();
System.out.println(vomServer.readLine()); // Gibt Antwort des Servers direkt aus
return;
} catch (IOException e) {
System.err.println("Probleme bei void Ping() (IOException)");
return;
......@@ -104,7 +117,8 @@ public class Client {
// Aufruf "echo":
private void echo(String eingabe) throws IOException{
try {
zumServer.write(eingabe); // Ganze Botschaft samt ECHO an Server geben
zumServer.write(eingabe+"\n");
zumServer.flush(); // Ganze Botschaft samt ECHO an Server geben
System.out.println(kuerzen(vomServer.readLine())); // Entfernen von "ECHO "
} catch (IOException e) {
System.err.println("Probleme bei void Echo() (IOException)");
......@@ -115,8 +129,11 @@ public class Client {
// Aufruf "current time":
private void time() throws IOException{
try {
zumServer.write("CURRENT TIME");
System.out.println(kuerzen(vomServer.readLine())); // Entfernen von "TIME "
zumServer.write("CURRENT TIME\n");
zumServer.flush();
System.out.println(vomServer.readLine()); // Entfernen von "TIME "
} catch (IOException e) {
System.err.println("Probleme bei void Time() (IOException)");
return;
......@@ -126,7 +143,8 @@ public class Client {
// Aufruf "current date":
private void date() throws IOException{
try {
zumServer.write("CURRENT DATE");
zumServer.write("CURRENT DATE\n");
zumServer.flush();
System.out.println(kuerzen(vomServer.readLine())); // Entfernen von "DATE "
} catch (IOException e) {
System.err.println("Probleme bei void Date() (IOException)");
......
......@@ -19,7 +19,7 @@ public class Server{
public Server() throws IOException
{
BufferedReader portinput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Server möchte den port:");
System.out.print("Serverport: ");
String givenPort = portinput.readLine();
portinput.close();
......@@ -45,42 +45,78 @@ public class Server{
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String command = "";
while (true) {
// Warte auf eine Nachricht vom Client:
String command = reader.readLine();
while(command == ""){
command = reader.readLine();
}
// Verarbeite den Befehl:
try {
if (command.equalsIgnoreCase("ping")) {
String[] words = command.split(" ");
switch (words[0].toLowerCase())
{
case "ping":
historyList.add(command);
PingPong();
} else if (command.substring(0, 4).equalsIgnoreCase("echo")) {
command ="";
break;
case "echo":
historyList.add(command);
Echo(command);
} else if (command.equalsIgnoreCase("current time")) {
command ="";
break;
case "current":
switch(words[1].toLowerCase())
{
case "time":
historyList.add(command);
sendTime("time");
} else if (command.equalsIgnoreCase("current date")) {
sendTime(words[1]);
command ="";
break;
case "date":
historyList.add(command);
sendTime("date");
} else if (command.equalsIgnoreCase("close")) {
historyList.removeAll(historyList);
socket.close();
serverSocket.close();
} else if (command.equalsIgnoreCase("history")) {
sendTime(words[1]);
command ="";
break;
default:
historyList.add(command);
History();
}
} catch (IOException Re) {
System.err.println("Error with commandsidk");
}
writer.write(words[0] + "Err Unsuported Format!\n");
writer.flush();
command ="";
break;
}
case "history":
historyList.add(command);
History();
command ="";
break;
case "close":
break;
default:
writer.write("Err Wrong or No command\n");
System.out.println("Err Wrong or No command");
writer.flush();
break;
}
} catch (IOException Re) {
System.err.println("Error with commands");
}
}
}
private void History()
{
......@@ -88,8 +124,10 @@ public class Server{
for(int i = 0;i < historyList.size();i++)
{
writer.write(i + ": " + historyList.get(i));
}
writer.write("HstrLstDone");
writer.flush();
}catch (IOException err)
{
System.err.println("History Error");
......@@ -106,13 +144,16 @@ public class Server{
case("time"):
SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm:ss");
String formatedStringTime = timeDateFormat.format(new Date());
writer.write(format.toUpperCase()+" "+formatedStringTime);
writer.write(format.toUpperCase()+" "+formatedStringTime+"\n");
writer.flush();
break;
case("date"):
SimpleDateFormat dateDateFormat = new SimpleDateFormat("dd.MM.yyyy");
String formatedStringDate = dateDateFormat.format(new Date());
writer.write(format.toUpperCase()+" "+formatedStringDate);
writer.write(format.toUpperCase()+" "+formatedStringDate+"\n");
writer.flush();
break;
default:
......@@ -123,7 +164,6 @@ public class Server{
}catch (IOException err)
{
System.err.println("ServerErr sendTime");
}
}
......@@ -131,7 +171,9 @@ public class Server{
private void Echo(String msg) throws IOException
{
try{
writer.write(msg);
System.out.println("received and sending back: " +msg);
writer.write(msg+"\n");
writer.flush();
return;
}catch (IOException err)
{
......@@ -144,9 +186,11 @@ public class Server{
private void PingPong() throws IOException {
try {
// Sende "Pong" an den Client:
System.out.println("wir haben ping Empfangen sende pong:");
System.out.println("received: PING sending: PONG");
writer.write("PONG\n");
writer.flush();
return;
} catch (IOException e) {
System.err.println("Problem bei void PingPong() (IOException)");
return;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment