Select Git revision
Server.java
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Server.java 5.89 KiB
package heimaufgaben;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.nio.*;
import java.security.*;
import java.util.*;
import java.text.*;
public class Server{
//ähnlich wie beimclient now:
private String ip = "";
private int port = 0;
private ServerSocket serverSocket;
private BufferedReader reader;
private BufferedWriter writer;
private ArrayList<String> historyList = new ArrayList<String>();
public Server() throws IOException
{
BufferedReader portinput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Serverport: ");
String givenPort = portinput.readLine();
portinput.close();
try {
port = Integer.parseInt(givenPort);
if (port < 1 || port > 65535) {
System.err.println("Err der angegebene Port ist ungültig (muss zwischen 1 und 65535 liegen)");
return;
}
} catch (NumberFormatException e) {
System.err.println("Err Format des Eingegebenen Ports ist Falsch");
return;
}
//PROBLEM -> reads from wrong socket so stalemate
serverSocket = new ServerSocket(port);
//Socket socket = new Socket(); old code
System.out.println("Server has created Socket ready to accept client");
Socket socket = serverSocket.accept();
System.out.println("Server has accepted Connection");
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String command = "";
while (true) {
// Warte auf eine Nachricht vom Client:
while(command == ""){
command = reader.readLine();
}
// Verarbeite den Befehl:
try {
String[] words = command.split(" ");
switch (words[0].toLowerCase())
{
case "ping":
historyList.add(command);
PingPong();
command ="";
break;
case "echo":
historyList.add(command);
Echo(command);
command ="";
break;
case "current":
switch(words[1].toLowerCase())
{
case "time":
historyList.add(command);
sendTime(words[1]);
command ="";
break;
case "date":
historyList.add(command);
sendTime(words[1]);
command ="";
break;
default:
historyList.add(command);
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()
{
try{
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");
}
}
private void sendTime(String format) throws IOException
{
try{
switch(format.toLowerCase())
{
case("time"):
SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm:ss");
String formatedStringTime = timeDateFormat.format(new Date());
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+"\n");
writer.flush();
break;
default:
writer.write("Unknown Format Requested: Known Formats are: Date || Time");
break;
}
}catch (IOException err)
{
System.err.println("ServerErr sendTime");
}
}
private void Echo(String msg) throws IOException
{
try{
System.out.println("received and sending back: " +msg);
writer.write(msg+"\n");
writer.flush();
return;
}catch (IOException err)
{
System.err.println("ServerErr Echo");
}
}
private void PingPong() throws IOException {
try {
// Sende "Pong" an den Client:
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;
}
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}