Skip to content
Snippets Groups Projects
Commit ccb2dcab authored by Philipp Lachowski's avatar Philipp Lachowski
Browse files

Client rein in Main (Aufgabe 4.1)

parent cdc83dc1
Branches
No related tags found
1 merge request!1Client rein in Main (Aufgabe 4.1)
......@@ -8,5 +8,4 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
</module>
\ No newline at end of file
......@@ -8,5 +8,129 @@ import java.util.*;
import java.text.*;
public class Client {
private String ip = "";
private int port = -1;
private Socket clientSocket;
private BufferedWriter zumServer;
private BufferedReader vomServer;
public Client() throws IOException{
// 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?");
ip = info.readLine();
System.out.println("Port?");
String portFragezeichen = info.readLine(); // Port ist vorerst String
info.close();
// Port muss vom String zum Int werden:
try{
port = Integer.parseInt(portFragezeichen);
} catch (NumberFormatException e) { // Falls er keine Zahl ist
System.err.println("Der Port ist keine Zahl!");
return;
}
// Wenn falsche IP oder port eingegeben wird:
if(!ip.equals("127.0.0.1")){
if(!ip.equals("localhost"))
{
System.err.println("Falsche IP!");
return;
}
}
if(port != 2022){
System.err.println("Falscher Port!");
return;
}
// Verbindungsversuch:
try {
clientSocket = new Socket(ip, port);
} catch (IOException e) {
System.err.println("Fehler bei clientSocket-Erstellung(IOException)\n-> Läuft der Server?");
return;
}
// Erschaffung von Kommunikationsverbindung:
try {
zumServer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
vomServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
System.err.println("Problem beim Bufferedwriter(IOException)");
return;
}
// Eingaben werden aufgenommen und bearbeitet bis "close":
System.out.println("Verbindung hergestellt, erwarte Auftrag:");
while(true)
{
String eingabe = info.readLine();
if(eingabe.equalsIgnoreCase("close")){
break;
}
else if(eingabe.equalsIgnoreCase("ping")){
ping();
}
else if(eingabe.substring(0,4).equalsIgnoreCase("echo")){
echo(eingabe);
}
else if(eingabe.equalsIgnoreCase("current time")){
time();
}
else if(eingabe.equalsIgnoreCase("current date")){
date();
}
}
// "close" -> While schleife endet, constructor endet, client ist beendet
}
// Aufruf "ping":
private void ping() throws IOException{
try {
zumServer.write("PING");
System.out.println(vomServer.readLine()); // Gibt Antwort des Servers direkt aus
} catch (IOException e) {
System.err.println("Probleme bei void Ping() (IOException)");
return;
}
}
// Aufruf "echo":
private void echo(String eingabe) throws IOException{
try {
zumServer.write(eingabe); // 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)");
return;
}
}
// Aufruf "current time":
private void time() throws IOException{
try {
zumServer.write("CURRENT TIME");
System.out.println(kuerzen(vomServer.readLine())); // Entfernen von "TIME "
} catch (IOException e) {
System.err.println("Probleme bei void Time() (IOException)");
return;
}
}
// Aufruf "current date":
private void date() throws IOException{
try {
zumServer.write("CURRENT DATE");
System.out.println(kuerzen(vomServer.readLine())); // Entfernen von "DATE "
} catch (IOException e) {
System.err.println("Probleme bei void Date() (IOException)");
return;
}
}
// Kürzen der ersten 5 Buchstaben, gebraucht für ECHO, TIME, DATE:
private String kuerzen(String eingabe) {
return eingabe.substring(5);
}
}
......@@ -7,13 +7,9 @@ import java.security.*;
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
Client client = new Client();
System.out.println("Der Client wurde beendet.");
}
}
......@@ -8,5 +8,4 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
</module>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment