From 251dc27c72b4612a19e7747eea8d4db5e99b9e7e Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 14 Mar 2022 13:56:49 +0100 Subject: [PATCH] moved BudaClientServerStuff --- .../executionHistory/executionHistory.lock | Bin 17 -> 17 bytes .gradle/6.9.2/fileHashes/fileHashes.lock | Bin 17 -> 17 bytes .../buildOutputCleanup.lock | Bin 17 -> 17 bytes .idea/misc.xml | 3 + .../BudaClientServerStuff}/BudaClient.java | 0 .../BudaClientThread.java | 0 .../BudaClientServerStuff}/BudaServer.java | 0 .../ClientListener.java | 0 .../ServerConnector.java | 0 .../BudaServerClientStuff/.idea/.gitignore | 0 .../BudaServerClientStuff/.idea/misc.xml | 0 .../BudaServerClientStuff/.idea/modules.xml | 0 .../BudaServerClientStuff.iml | 0 .../BudaServerClientStuff/README.txt | 0 .../BudaServerClientStuff/src/BudaClient.java | 29 +++++++ .../src/BudaClientThread.java | 73 ++++++++++++++++++ .../BudaServerClientStuff/src/BudaServer.java | 25 ++++++ .../src/ClientListener.java | 25 ++++++ .../src/ServerConnector.java | 27 +++++++ 19 files changed, 182 insertions(+) rename {Meilenstein I/BudaServerClientStuff/src => src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff}/BudaClient.java (100%) rename {Meilenstein I/BudaServerClientStuff/src => src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff}/BudaClientThread.java (100%) rename {Meilenstein I/BudaServerClientStuff/src => src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff}/BudaServer.java (100%) rename {Meilenstein I/BudaServerClientStuff/src => src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff}/ClientListener.java (100%) rename {Meilenstein I/BudaServerClientStuff/src => src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff}/ServerConnector.java (100%) rename {Meilenstein I => Übung}/BudaServerClientStuff/.idea/.gitignore (100%) rename {Meilenstein I => Übung}/BudaServerClientStuff/.idea/misc.xml (100%) rename {Meilenstein I => Übung}/BudaServerClientStuff/.idea/modules.xml (100%) rename {Meilenstein I => Übung}/BudaServerClientStuff/BudaServerClientStuff.iml (100%) rename {Meilenstein I => Übung}/BudaServerClientStuff/README.txt (100%) create mode 100644 Übung/BudaServerClientStuff/src/BudaClient.java create mode 100644 Übung/BudaServerClientStuff/src/BudaClientThread.java create mode 100644 Übung/BudaServerClientStuff/src/BudaServer.java create mode 100644 Übung/BudaServerClientStuff/src/ClientListener.java create mode 100644 Übung/BudaServerClientStuff/src/ServerConnector.java diff --git a/.gradle/6.9.2/executionHistory/executionHistory.lock b/.gradle/6.9.2/executionHistory/executionHistory.lock index ad293efc610e70f1372ea4a2693c6fda105efd04..c32237ffd1bbf57955900b62dfc396d6b44120e3 100644 GIT binary patch literal 17 UcmZRsPI%sX_vc08Oa|f&c&j literal 17 UcmZRcaBu(1OMiRzFhGDJ08M!YQUCw| diff --git a/.idea/misc.xml b/.idea/misc.xml index 266fe31..f9cb75b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + + + \ No newline at end of file diff --git a/Meilenstein I/BudaServerClientStuff/src/BudaClient.java b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/BudaClient.java similarity index 100% rename from Meilenstein I/BudaServerClientStuff/src/BudaClient.java rename to src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/BudaClient.java diff --git a/Meilenstein I/BudaServerClientStuff/src/BudaClientThread.java b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/BudaClientThread.java similarity index 100% rename from Meilenstein I/BudaServerClientStuff/src/BudaClientThread.java rename to src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/BudaClientThread.java diff --git a/Meilenstein I/BudaServerClientStuff/src/BudaServer.java b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/BudaServer.java similarity index 100% rename from Meilenstein I/BudaServerClientStuff/src/BudaServer.java rename to src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/BudaServer.java diff --git a/Meilenstein I/BudaServerClientStuff/src/ClientListener.java b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/ClientListener.java similarity index 100% rename from Meilenstein I/BudaServerClientStuff/src/ClientListener.java rename to src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/ClientListener.java diff --git a/Meilenstein I/BudaServerClientStuff/src/ServerConnector.java b/src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/ServerConnector.java similarity index 100% rename from Meilenstein I/BudaServerClientStuff/src/ServerConnector.java rename to src/main/java/ch/unibas/dmi/dbis/cs108/BudaClientServerStuff/ServerConnector.java diff --git a/Meilenstein I/BudaServerClientStuff/.idea/.gitignore b/Übung/BudaServerClientStuff/.idea/.gitignore similarity index 100% rename from Meilenstein I/BudaServerClientStuff/.idea/.gitignore rename to Übung/BudaServerClientStuff/.idea/.gitignore diff --git a/Meilenstein I/BudaServerClientStuff/.idea/misc.xml b/Übung/BudaServerClientStuff/.idea/misc.xml similarity index 100% rename from Meilenstein I/BudaServerClientStuff/.idea/misc.xml rename to Übung/BudaServerClientStuff/.idea/misc.xml diff --git a/Meilenstein I/BudaServerClientStuff/.idea/modules.xml b/Übung/BudaServerClientStuff/.idea/modules.xml similarity index 100% rename from Meilenstein I/BudaServerClientStuff/.idea/modules.xml rename to Übung/BudaServerClientStuff/.idea/modules.xml diff --git a/Meilenstein I/BudaServerClientStuff/BudaServerClientStuff.iml b/Übung/BudaServerClientStuff/BudaServerClientStuff.iml similarity index 100% rename from Meilenstein I/BudaServerClientStuff/BudaServerClientStuff.iml rename to Übung/BudaServerClientStuff/BudaServerClientStuff.iml diff --git a/Meilenstein I/BudaServerClientStuff/README.txt b/Übung/BudaServerClientStuff/README.txt similarity index 100% rename from Meilenstein I/BudaServerClientStuff/README.txt rename to Übung/BudaServerClientStuff/README.txt diff --git a/Übung/BudaServerClientStuff/src/BudaClient.java b/Übung/BudaServerClientStuff/src/BudaClient.java new file mode 100644 index 0000000..0c74c5b --- /dev/null +++ b/Übung/BudaServerClientStuff/src/BudaClient.java @@ -0,0 +1,29 @@ + +import java.io.*; +import java.net.Socket; + +public class BudaClient { + public static void main(String[] args) { + Socket sock = null; + try { + sock = new Socket("localhost", 8090); + OutputStream out= sock.getOutputStream(); + BufferedReader conin = new BufferedReader(new InputStreamReader(System.in)); + String line = ""; + while (true) { + line = conin.readLine(); + out.write(line.getBytes()); + if (line.equalsIgnoreCase("Quitx")) { + break; + } + //line.startsWith() //todo: automatically handle name lengths + //TODO: Implement inputStream in. + } + + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/Übung/BudaServerClientStuff/src/BudaClientThread.java b/Übung/BudaServerClientStuff/src/BudaClientThread.java new file mode 100644 index 0000000..9bd8066 --- /dev/null +++ b/Übung/BudaServerClientStuff/src/BudaClientThread.java @@ -0,0 +1,73 @@ + + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +public class BudaClientThread implements Runnable { + int number; + Socket socket; + String name; + + + public BudaClientThread(int number, Socket socket) { + this.number = number; + this.socket = socket; + name = ""; + } + + public void run() { + System.out.println("Connection " + number + " established."); + try { + InputStream in = socket.getInputStream(); + OutputStream out = socket.getOutputStream(); + byte[] command; + String comString; + while (true) { + command = new byte[5]; + System.out.println("Waiting to receive a line"); + in.read(command); + System.out.println("Got a line!"); + comString = new String(command); + System.out.println("Client number " + number + " sent this message: " + comString); + if (comString.equalsIgnoreCase("Quitx")) { + BudaServer.quit = true; + System.out.println("I just set quit to true!"); + break; + } + + //todo: do as switch. + + if (comString.equalsIgnoreCase("NAME:")) { //todo: implement these as methods? + setName(in); + } + + if (comString.equalsIgnoreCase("NAMES")) { + printnames(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void printnames() { + for (BudaClientThread t: BudaServer.Clients) { + System.out.println(t.name + " connected (#" + t.number + ")"); + } + } + + public void setName(InputStream in) throws IOException { + //byte[] namebyte = new byte[0]; + String nameString = ""; + int i; + while (true) { + i = in.read(); + if (i == 46) break; + nameString = nameString + (char) i; + } + this.name = nameString; + } + +} diff --git a/Übung/BudaServerClientStuff/src/BudaServer.java b/Übung/BudaServerClientStuff/src/BudaServer.java new file mode 100644 index 0000000..6e12148 --- /dev/null +++ b/Übung/BudaServerClientStuff/src/BudaServer.java @@ -0,0 +1,25 @@ + + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.HashSet; + +public class BudaServer { + public static boolean quit = false; //todo: meaningfully implement this + public static HashSet Clients = new HashSet(); + static int connections = 0; + + public static void main(String[] args) { + ServerConnector ServC = new ServerConnector(); + Thread ServCThread = new Thread(ServC); + ServCThread.start(); + System.out.println("Server has entered its main loop"); + while (!quit) { + //Main server stuff goes here + } + //ServCThread.stop(); //todo: find some alternative for this. + System.out.println("stopping the main BudaServer thread."); + System.out.println("Quitting after the next connection is made."); + } +} diff --git a/Übung/BudaServerClientStuff/src/ClientListener.java b/Übung/BudaServerClientStuff/src/ClientListener.java new file mode 100644 index 0000000..6fe4639 --- /dev/null +++ b/Übung/BudaServerClientStuff/src/ClientListener.java @@ -0,0 +1,25 @@ +import java.io.*; +import java.net.Socket; + +public class ClientListener implements Runnable{ + private final Socket sock; + + public ClientListener(Socket sock) { + this.sock = sock; + } + + public void run(){ + byte[] command = new byte[5]; + String comString; + + try { + InputStream in = sock.getInputStream(); + in.read(command); + System.out.println("Got a line!"); + comString = new String(command); + } catch (IOException e) { + e.printStackTrace(); + + } + } +} diff --git a/Übung/BudaServerClientStuff/src/ServerConnector.java b/Übung/BudaServerClientStuff/src/ServerConnector.java new file mode 100644 index 0000000..9833005 --- /dev/null +++ b/Übung/BudaServerClientStuff/src/ServerConnector.java @@ -0,0 +1,27 @@ +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class ServerConnector implements Runnable{ + public void run() { + try { + System.out.println( + "Warte auf Verbindungen auf Port 8090..."); + ServerSocket servSock = new ServerSocket(8090); + while (true) { + Socket socket = servSock.accept(); + System.out.println("got a connection: socket " + BudaServer.connections + socket.toString()); + BudaClientThread newClientThread = new BudaClientThread(++BudaServer.connections, socket); + BudaServer.Clients.add(newClientThread); + Thread bCT = new Thread(newClientThread); + bCT.start(); + } + } catch (IOException e) { + System.out.println("server got an error"); + System.err.println(e); + System.exit(1); + } + + + } +}