handle exceptions
This commit is contained in:
parent
37ece02bd7
commit
4ac130b9d2
18
Main.java
18
Main.java
|
@ -12,15 +12,29 @@ import java.util.List;
|
|||
|
||||
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)) {
|
||||
System.out.println("listening on socket 8080...");
|
||||
while (true) {
|
||||
// .accept() blocks until we have a client
|
||||
try (Socket client = serverSocket.accept()) {
|
||||
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;
|
||||
while (!(line = br.readLine()).isBlank()) {
|
||||
requestBuilder.append(line + "\r\n");
|
||||
|
||||
}
|
||||
|
||||
String request = requestBuilder.toString();
|
||||
// System.out.println(request);
|
||||
// PARSE THE REQUEST
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<<head>
|
||||
<head>
|
||||
<header>
|
||||
<title>JAVA SERVER TEST</title>
|
||||
</header>
|
||||
|
|
Loading…
Reference in New Issue