/q now works without throwing any errors

This commit is contained in:
Jonas 2022-04-04 14:36:29 +02:00
parent a5c3ad7b79
commit 033a7e3bb6
3 changed files with 36 additions and 17 deletions

View File

@ -29,11 +29,10 @@ public class Client {
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.in = new BufferedReader((new InputStreamReader((socket.getInputStream())))); this.in = new BufferedReader((new InputStreamReader((socket.getInputStream()))));
this.userName = userName; this.userName = userName;
sendMsgToServer(getUsername()); sendMsgToServer(getUsername()); //todo: dont just send username directly pls
clientPinger = new ClientPinger(this, this.socket); clientPinger = new ClientPinger(this, this.socket);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in, out);
} }
} }
@ -44,13 +43,24 @@ public class Client {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Scanner sc = new Scanner(System.in); //Scanner sc = new Scanner(System.in);
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
while (socket.isConnected() && !socket.isClosed()) { while (socket.isConnected() && !socket.isClosed()) {
String msg = sc.nextLine(); try {
String formattedMSG = MessageFormatter.formatMsg(msg); if (bfr.ready()) {
sendMsgToServer(formattedMSG); String msg = bfr.readLine();
String formattedMSG = MessageFormatter.formatMsg(msg);
sendMsgToServer(formattedMSG);
}
Thread.sleep(20);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
//LOGGER.debug("just checked next line");
} }
LOGGER.debug("userInputListener is done"); //LOGGER.debug("userInputListener is done");
} }
}).start(); }).start();
} }
@ -71,17 +81,16 @@ public class Client {
while (socket.isConnected() && !socket.isClosed()) { while (socket.isConnected() && !socket.isClosed()) {
try { try {
chatMsg = in.readLine(); chatMsg = in.readLine(); //todo: maybe if
if (chatMsg != null) { if (chatMsg != null) {
parse(chatMsg); //todo: i think this trows an error BC chatMsg is null if client disconnects parse(chatMsg); //todo: i think this trows an error BC chatMsg is null if client disconnects
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
closeEverything(socket, in, out);
} }
} }
LOGGER.debug("chatListener is done"); //LOGGER.debug("chatListener is done");
} }
}).start(); }).start();
} }
@ -111,9 +120,8 @@ public class Client {
JClientProtocolParser.parse(msg, this); JClientProtocolParser.parse(msg, this);
} }
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) { public void closeEverything() {
//TODO Correctly closing a clients connection //TODO Correctly closing a clients connection
try { try {
if (in != null) { if (in != null) {
in.close(); in.close();
@ -123,7 +131,9 @@ public class Client {
} }
if (socket != null) { if (socket != null) {
socket.close(); socket.close();
//LOGGER.debug("closed the socket!");
} }
System.out.println("Disconnected from server.");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -162,4 +172,16 @@ public class Client {
public String getUsername() { public String getUsername() {
return userName; return userName;
} }
public Socket getSocket() {
return socket;
}
public BufferedReader getIn() {
return in;
}
public BufferedWriter getOut() {
return out;
}
} }

View File

@ -39,7 +39,7 @@ public class JClientProtocolParser {
System.out.println(msg.substring(6)); System.out.println(msg.substring(6));
break; break;
case "QUITC": case "QUITC":
//c.closeEverything(); todo: this line. c.closeEverything();
System.out.println("bye!"); System.out.println("bye!");
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");

View File

@ -103,7 +103,7 @@ public class ClientHandler implements Runnable {
} }
/** /**
* Lets the client change their respective username, if the username is already taken, a similar * Lets the client change their username, if the username is already taken, a similar
* option is chosen. * option is chosen.
* *
* @param newName The desired new name to replace the old one with. * @param newName The desired new name to replace the old one with.
@ -155,9 +155,6 @@ public class ClientHandler implements Runnable {
/** /**
* Does exactly what it says on the tin, closes all connections of Client to Server. * Does exactly what it says on the tin, closes all connections of Client to Server.
* *
* @param socket the socket to be closed
* @param in the in-Stream reader to be closed
* @param out the out-Stream Write to be closed
*/ */
public void disconnectClient() { public void disconnectClient() {
sendMsgToClient("QUITC"); sendMsgToClient("QUITC");