Skip to content
Snippets Groups Projects
Commit d3eda617 authored by Elrabu's avatar Elrabu
Browse files

Update: Server keeps running, all errors caught.

parent 7fa132f9
No related branches found
No related tags found
2 merge requests!35Update: Added more Stuff to be transported over the server.,!2Merge of Multiplayer Feature into development branch
...@@ -22,8 +22,7 @@ public class Client { ...@@ -22,8 +22,7 @@ public class Client {
public String sendMessage(String msg) throws IOException { public String sendMessage(String msg) throws IOException {
out.println(msg); out.println(msg);
String resp = in.readLine(); return in.readLine();
return resp;
} }
public void stopConnection() throws IOException { public void stopConnection() throws IOException {
......
...@@ -10,9 +10,5 @@ public class ConnectionHandling { ...@@ -10,9 +10,5 @@ public class ConnectionHandling {
String resp1 = client.sendMessage("hello"); String resp1 = client.sendMessage("hello");
System.out.println(resp1); System.out.println(resp1);
Client client1 = new Client();
client1.startConnection("localhost", 4444);
String resp2 = client1.sendMessage("hello");
System.out.println(resp2);
} }
} }
...@@ -25,6 +25,8 @@ public class Server { ...@@ -25,6 +25,8 @@ public class Server {
serverSocket.close(); serverSocket.close();
} }
// EchoClientHandler class in the Server
private static class EchoClientHandler extends Thread { private static class EchoClientHandler extends Thread {
private Socket clientSocket; private Socket clientSocket;
private PrintWriter out; private PrintWriter out;
...@@ -37,49 +39,43 @@ public class Server { ...@@ -37,49 +39,43 @@ public class Server {
public void run() { public void run() {
try { try {
out = new PrintWriter(clientSocket.getOutputStream(), true); out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) { in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
throw new RuntimeException(e);
}
try {
in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
String inputLine; String inputLine;
try { while (clientSocket.isConnected() && (inputLine = in.readLine()) != null) {
while ((inputLine = in.readLine()) != null) {
System.out.println("Received message from client: " + inputLine); System.out.println("Received message from client: " + inputLine);
try {
if ((inputLine = in.readLine()) == null) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
if (".".equals(inputLine)) { if (".".equals(inputLine)) {
out.println("bye"); out.println("bye");
out.flush();
System.out.println("Sent response to client: bye");
break; break;
} }
out.println(inputLine); out.println(inputLine);
out.flush(); out.flush();
System.out.println("Sent response to client: " + inputLine);
} }
} catch (SocketException e) {
// Connection reset by client; no need to print a stack trace
System.out.println("Client connection reset.");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); // Other IOExceptions - log or handle as needed
e.printStackTrace();
} finally {
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
// Log the exception or handle as appropriate for your application
e.printStackTrace();
}
} }
}
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
out.close();
try {
clientSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment