Compare commits

..

No commits in common. "61777d55818f6acfd3f4abf06560dd740bb2df35" and "37ece02bd742fc72f7f4291d850933c2b1671c3e" have entirely different histories.

3 changed files with 4 additions and 28 deletions

View File

@ -1,9 +0,0 @@
<html>
<head>
<title>404</title>
</head>
<body>
<h1>UH-OH</h1>
<p>The page you were looking for could not be found.</p>
</body>
</html>

View File

@ -12,29 +12,15 @@ import java.util.List;
public class Main {
public static void main(String[] args) {
int attempts = 1;
public static void main(String[] args) throws Exception {
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) {
}
}
}
@ -47,9 +33,7 @@ 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
@ -79,7 +63,8 @@ public class Main {
sendResponse(client, "200 OK", contentType, Files.readAllBytes(filepath));
} else {
// 404
sendResponse(client, "404 Not Found", "text/html", Files.readAllBytes(getFilePath("404.html")));
byte[] notFoundContent = "<h1>404 Something went wrong</h1>".getBytes();
sendResponse(client, "404 Not Found", "text/html", notFoundContent);
}
}

View File

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