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

Update: Server keeps running, all errors caught.

parent 09ae1284
No related branches found
No related tags found
3 merge requests!74V1,!73Initial commit,!71Merge DataBase into Development
......@@ -22,8 +22,7 @@ public class Client {
public String sendMessage(String msg) throws IOException {
out.println(msg);
String resp = in.readLine();
return resp;
return in.readLine();
}
public void stopConnection() throws IOException {
......
......@@ -10,9 +10,5 @@ public class ConnectionHandling {
String resp1 = client.sendMessage("hello");
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 {
serverSocket.close();
}
// EchoClientHandler class in the Server
private static class EchoClientHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
......@@ -37,49 +39,43 @@ public class Server {
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
while (clientSocket.isConnected() && (inputLine = in.readLine()) != null) {
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)) {
out.println("bye");
out.flush();
System.out.println("Sent response to client: bye");
break;
}
out.println(inputLine);
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) {
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