Cleaned up & polished BudaClientServerStuff

This commit is contained in:
Jonas 2022-03-16 16:07:17 +01:00
parent bdcdd1d025
commit c74711ae07
5 changed files with 20 additions and 21 deletions

View File

@ -1,3 +1,4 @@
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
import java.io.*;
import java.net.Socket;
@ -9,7 +10,7 @@ public class BudaClient {
sock = new Socket("localhost", 8090);
OutputStream out= sock.getOutputStream();
BufferedReader conin = new BufferedReader(new InputStreamReader(System.in));
String line = "";
String line = ""; //this String is the line that will be sent to the server
while (true) {
line = conin.readLine();
out.write(line.getBytes());

View File

@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
import java.io.IOException;
import java.io.InputStream;
@ -26,25 +26,18 @@ public class BudaClientThread implements Runnable {
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!");
in.read(command); //BudaClientThread waits to receive a line from the inputstream.
comString = new String(command);
System.out.println("Client number " + number + " sent this message: " + comString);
if (comString.equalsIgnoreCase("Quitx")) {
if (comString.equalsIgnoreCase("Quitx")) { //todo: do as switch.
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?
} else if (comString.equalsIgnoreCase("NAME:")) {
setName(in);
}
if (comString.equalsIgnoreCase("NAMES")) {
} else if (comString.equalsIgnoreCase("NAMES")) {
printnames();
} else {
System.out.println("Client number " + number + " sent this message: \"" + comString + "\" and I'm not sure what it means.");
}
}
} catch (IOException e) {
@ -54,20 +47,20 @@ public class BudaClientThread implements Runnable {
public void printnames() {
for (BudaClientThread t: BudaServer.Clients) {
System.out.println(t.name + " connected (#" + t.number + ")");
System.out.println("user named "+ t.name + " is 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;
if (i == 46) break; //the name ends with a "."
nameString = nameString + (char) i;
}
this.name = nameString;
System.out.println("Client number " + number + " changed their name to: " + nameString);
}
}

View File

@ -1,4 +1,4 @@
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
import java.io.IOException;
import java.net.ServerSocket;
@ -13,8 +13,7 @@ public class BudaServer {
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");
ServCThread.start(); //the ServCThread listens for new connections so the server can do other things
while (!quit) {
//Main server stuff goes here
}

View File

@ -1,6 +1,10 @@
This is a demo of some (basic) client / server functionality.
Run BudaClient.java for the Client and BudaServer.java for the Server. Everything connects locally via port 8090
The client reads from the console input and sends that to the server. The Server generally reads messages in five characters (so a text-based protocol could be based on commands of five characters).
The server can connect to an arbitrary number of clients.
Type "Name:Jonas B." to change the client's name to Jonas B (the "." is where the name stops).
Type "Names" for the Server to display everyone who is connected along with their name (if they have set a name).

View File

@ -1,3 +1,5 @@
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;