handle exceptions

This commit is contained in:
andrzej 2024-11-11 12:21:25 +01:00
parent 37ece02bd7
commit 4ac130b9d2
2 changed files with 18 additions and 2 deletions

View File

@ -12,15 +12,29 @@ import java.util.List;
public class Main { public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) {
int attempts = 1;
try (ServerSocket serverSocket = new ServerSocket(8080)) { try (ServerSocket serverSocket = new ServerSocket(8080)) {
System.out.println("listening on socket 8080..."); System.out.println("listening on socket 8080...");
while (true) { while (true) {
// .accept() blocks until we have a client // .accept() blocks until we have a client
try (Socket client = serverSocket.accept()) { try (Socket client = serverSocket.accept()) {
handleClient(client); handleClient(client);
} catch (IOException e) {
System.out.printf("Error accepting client: %s\n", e);
} }
} }
} catch (IOException e) {
System.out.printf("Error opening port (attempt %s) %s:\n", attempts, e);
if (attempts > 3) {
System.out.println("This isn't working. Shutting down.");
System.exit(1);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
} }
} }
@ -33,7 +47,9 @@ public class Main {
String line; String line;
while (!(line = br.readLine()).isBlank()) { while (!(line = br.readLine()).isBlank()) {
requestBuilder.append(line + "\r\n"); requestBuilder.append(line + "\r\n");
} }
String request = requestBuilder.toString(); String request = requestBuilder.toString();
// System.out.println(request); // System.out.println(request);
// PARSE THE REQUEST // PARSE THE REQUEST

View File

@ -1,4 +1,4 @@
<<head> <head>
<header> <header>
<title>JAVA SERVER TEST</title> <title>JAVA SERVER TEST</title>
</header> </header>