Cleaned up & polished BudaClientServerStuff
This commit is contained in:
parent
bdcdd1d025
commit
c74711ae07
@ -1,3 +1,4 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@ -9,7 +10,7 @@ public class BudaClient {
|
|||||||
sock = new Socket("localhost", 8090);
|
sock = new Socket("localhost", 8090);
|
||||||
OutputStream out= sock.getOutputStream();
|
OutputStream out= sock.getOutputStream();
|
||||||
BufferedReader conin = new BufferedReader(new InputStreamReader(System.in));
|
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) {
|
while (true) {
|
||||||
line = conin.readLine();
|
line = conin.readLine();
|
||||||
out.write(line.getBytes());
|
out.write(line.getBytes());
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -26,25 +26,18 @@ public class BudaClientThread implements Runnable {
|
|||||||
String comString;
|
String comString;
|
||||||
while (true) {
|
while (true) {
|
||||||
command = new byte[5];
|
command = new byte[5];
|
||||||
System.out.println("Waiting to receive a line");
|
in.read(command); //BudaClientThread waits to receive a line from the inputstream.
|
||||||
in.read(command);
|
|
||||||
System.out.println("Got a line!");
|
|
||||||
comString = new String(command);
|
comString = new String(command);
|
||||||
System.out.println("Client number " + number + " sent this message: " + comString);
|
if (comString.equalsIgnoreCase("Quitx")) { //todo: do as switch.
|
||||||
if (comString.equalsIgnoreCase("Quitx")) {
|
|
||||||
BudaServer.quit = true;
|
BudaServer.quit = true;
|
||||||
System.out.println("I just set quit to true!");
|
System.out.println("I just set quit to true!");
|
||||||
break;
|
break;
|
||||||
}
|
} else if (comString.equalsIgnoreCase("NAME:")) {
|
||||||
|
|
||||||
//todo: do as switch.
|
|
||||||
|
|
||||||
if (comString.equalsIgnoreCase("NAME:")) { //todo: implement these as methods?
|
|
||||||
setName(in);
|
setName(in);
|
||||||
}
|
} else if (comString.equalsIgnoreCase("NAMES")) {
|
||||||
|
|
||||||
if (comString.equalsIgnoreCase("NAMES")) {
|
|
||||||
printnames();
|
printnames();
|
||||||
|
} else {
|
||||||
|
System.out.println("Client number " + number + " sent this message: \"" + comString + "\" and I'm not sure what it means.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -54,20 +47,20 @@ public class BudaClientThread implements Runnable {
|
|||||||
|
|
||||||
public void printnames() {
|
public void printnames() {
|
||||||
for (BudaClientThread t: BudaServer.Clients) {
|
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 {
|
public void setName(InputStream in) throws IOException {
|
||||||
//byte[] namebyte = new byte[0];
|
|
||||||
String nameString = "";
|
String nameString = "";
|
||||||
int i;
|
int i;
|
||||||
while (true) {
|
while (true) {
|
||||||
i = in.read();
|
i = in.read();
|
||||||
if (i == 46) break;
|
if (i == 46) break; //the name ends with a "."
|
||||||
nameString = nameString + (char) i;
|
nameString = nameString + (char) i;
|
||||||
}
|
}
|
||||||
this.name = nameString;
|
this.name = nameString;
|
||||||
|
System.out.println("Client number " + number + " changed their name to: " + nameString);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
@ -13,8 +13,7 @@ public class BudaServer {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ServerConnector ServC = new ServerConnector();
|
ServerConnector ServC = new ServerConnector();
|
||||||
Thread ServCThread = new Thread(ServC);
|
Thread ServCThread = new Thread(ServC);
|
||||||
ServCThread.start();
|
ServCThread.start(); //the ServCThread listens for new connections so the server can do other things
|
||||||
System.out.println("Server has entered its main loop");
|
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
//Main server stuff goes here
|
//Main server stuff goes here
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
This is a demo of some (basic) client / server functionality.
|
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
|
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 "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).
|
Type "Names" for the Server to display everyone who is connected along with their name (if they have set a name).
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.BudaClientServerStuff;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|||||||
Reference in New Issue
Block a user